pTutorial

New way to learn PHP

PHP MYSQL where clause

How to used where clause

I am going to explain the concept of where clause in database. Where clause is used to fetch particular record from the database.


Suppose a compnay database that have store employs record. Consider the query find all the member which salary amount are greater than $10000, this query written as.

Example

SELECT * FROM employ
where salary > 10000


In another word SELECT data from table as per your condition using where clause. There are some query using where clause are following bewlow.

SELECT * FROM employ
where name = john

SELECT * FROM employ
where name = john and salary =20000

Syntax

SELECT * FROM table_name
where field_name<=1000

Another Syntax

SELECT field_name(s)
FROM table_name
where field_name<=1000

Example of where clause

<?php

$db = @mysql_connect("locahost","root","");
mysql_select_db("my_db1") or die ("database selection failed");
echo "Connected to the database<br>";
$query="SELECT *from  student
where id = 2";
$a=mysql_query($query,$db) or die("fail to execute query!!!");
if(! $a )
{
die('Could not get data: ' . mysql_error());
}
echo "############################";
while($row = mysql_fetch_array($a))
{
	echo "
id:{$row['id']}"; echo "<br>Name :{$row['name']} <br> ". "Branch: {$row['branch']} <br> "; } echo "############################"; echo"<br>Query Fetched"; mysql_close($db); ?>

Output

Connected to the database
############################
id:2
Name :xyz
Branch: ma
############################
Query Fetched

Another example of where clause

<?php

$user = "root";
$pwd = "";
$host = "localhost";
$db = @mysql_connect($host,$user,$pwd);
mysql_select_db("my_db1");

$query="SELECT *from  student
where id = 2 or branch='cse' ";
$a=mysql_query($query,$db);
if(! $a )
{
die('Could not get data: ' . mysql_error());
}
echo "############################";
while($row = mysql_fetch_array($a))
{
	echo "
id:{$row['id']}"; echo "<br>Name :{$row['name']} <br> ". "Branch: {$row['branch']} <br> "; } echo "############################"; mysql_close($db); ?>

QUOTES OF THE DAY

Crafty men condemn studies, simple men admire them, and wise use them.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER