PHP MySQL Where Clause Example

PHP MySQL Where Clause Example


Posted in : PHP Posted on : February 4, 2011 at 5:23 PM Comments : [ 0 ]

In the php application, if you want to fetch data(records) that fulfill some specified criterion then use "where" clause.

PHP MySQL Where Clause Example:

In the php application, if you want to fetch data(records) that fulfill some specified criterion then use "where" clause. The where clause used for filter records on the basis of some conditions.

Syntax:

SELECT column_name FROM table_name
WHERE column_name specify_value

Example:

<?php
$connection = mysql_connect("localhost","root","root");

if (!$connection) {
die('PHP Mysql database connection could not connect : ' . mysql_error());
}
else{ 
$db_name = "php_mysql";

mysql_select_db($db_name, $connection);
$sql = "select * from user where user_id=2;";
$records = mysql_query($sql, $connection); 
while($rows = mysql_fetch_array($records)){
echo "User Id : " . $rows['user_id'] . "<br />";
echo "User Name : " . $rows['user_name'] . "<br />";
echo "User Email : " . $rows['user_email'] . "<br />";
echo "User Address : " . $rows['user_address'] . "<br /><br /><br />";
}
}
mysql_close($connection); 
?> 

In the given example we have fetch the record on the basis of the user_id  field that is unique in the database table.

The database user table data(records) is:

After running this example the output is:

Output:

User Id : 2
User Name : Brijesh
User Email : brijesh@gmail.com
User Address : Delhi

Download Example Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics