Breaking

LightBlog

Saturday 4 July 2015

MySQL Where Clause in PHP

MySQL Where Clause in PHP

If we want to select only specific column in table then we can use the WHERE Keyword in SQL. We have seen SELECT Command to fetch data from MySQL table. We can use a conditional clause called WHERE Clause to filter out results. Using WHERE Clause we can specify   a selection criteria to select a specific record from a table.
                                              
MySQL Where Clause in PHP

Syntax:  Select field1, field2, field3 from table_name where conditon1 or condition 2

The WHERE clause works like an if condition in any programming language. This clause is used to compare given value with the field value with the field value available in MySQL Table There are some operator used in WHERE clause.
Assume field A holds 20 and field B holds 30.
Operator
Description
Example
=
Checks both value they are equal or not equal
(A=B) is not true
!=
Checks both value they are equal or not equal if they not equal then condition is true
(A!=B) is true
Checks if the value of left operand is greater then right operand if yes then condition is true
(A>B) is not true
Checks if the value of left operand is less then right operand if yes then condition is true
(A<B ) is not true
>=
Checks if the value of left operand is greater then or equal to the right operand if yes then condition is true
(A>=B) is not true
<=
Checks if the value of left operand is less then or equal to right operand if yes then condition is true
(A<=B) is true


Example:
<?php
$con=mysql_connect(“localhost”, “deep “, 12345);
if (!$con)
{
die(“could not be connect the database” . mysql_error());
}
echo “ connected successfully “;
mysql_select_db ( “student”);
$sql = ”select stu_id, stu_name, stu_class from student1 where  stu_id=10;
$retrive =mysql_query ($sql, $con );
If (!$retrive)
{
die (“could not be connect”);
}
While ($row = mysql_fetch_array ($retrive))
{
echo  “ Student id is : {$row [‘stu_id’] } <br>”.
“ Student Name is : {$row [‘stu_name’] } <br>”.
“ Student Class is : {$row [‘stu_class’] } <br>” ;
}
mysql_close ($con);
?>
Adbox