WHERE
clause is used to extract only those records that fulfill a specified condition. SELECT column_name FROM table_name WHERE column_name operator value
The SQL query using the WHERE
clause in SELECT
statement, after that we'll execute this query through passing it to the PHP mysqli_query()
function to get the filtered data.
You can use one or more tables separated by comma to include various conditions using a WHERE
clause, but WHERE
clause is an optional part of SELECT
command.
You can specify more than one conditions using AND or OR operators.
A WHERE
clause can be used along with DELETE
or UPDATE
SQL command also to specify a condition.
The WHERE
clause works like an if condition in any programming language. This clause is used to compare given value with the field value available in MySQLi
table.
id | Name | College | Email Id | Mobile |
---|---|---|---|---|
1 | V V Ramana Reddy | S V Arts and Science College | info@freetimelearning.com | 9963666068 |
2 | Chanti | Vidhyalaya Degree College | freetimelearn@gmail.com | 9010023210 |
3 | Ramana | S V University | vvramanareddy9963@gmail.com | 9966463846 |
4 | Suresh | Aadhishankara | sureshreddy@gmail.com | 9700900292 |
5 | Suman Reddy | Priyadharshini | sumanreddy.mca@gmail.com | 9966395199 |
The following example will return all the records from student_details_2 table for which stu_name is Chanti :
<?php
$ftl_connect = mysqli_connect("localhost", "root", "", "ftl_db");
// Check connection
if($ftl_connect === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM student_details_2 WHERE stu_name='Chanti'";
if($result = mysqli_query($ftl_connect, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table table-bordered'>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>Name</th>";
echo "<th>College</th>";
echo "<th>Email Id</th>";
echo "<th>Mobile</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['stu_id'] . "</td>";
echo "<td>" . $row['stu_name'] . "</td>";
echo "<td>" . $row['college'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['mobile'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
} else{
echo "No records matching your query.";
}
} else{
echo "Error - could not able to execute $sql. " . mysqli_error($ftl_connect);
}
// Close connection
mysqli_close($ftl_connect);
?>
id | Name | College | Email Id | Mobile |
---|---|---|---|---|
2 | Chanti | Vidhyalaya Degree College | freetimelearn@gmail.com | 9010023210 |