November 28, 2026
How to filter data in select query using PHP?
Where clause is very powerful tool for making good MySQL queries. Through where clause you can filter records through your queries, you can get records as you want. The syntax of where clause is mentioned below:
Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
Example mentioned below:
Code
<?php
// Establish database connection
$connection = mysqli_connect("localhost", "marcus", "xyz123", "my_database");
// Check connection
if (!$connection)
{
die("Could not connect with MySQL: " . mysqli_connect_error());
}
// Execute SELECT query with WHERE condition
$result = mysqli_query($connection, 'SELECT * FROM Employee WHERE FirstName = "Edge"');
// Check query execution
if (!$result)
{
die("Error occurred: " . mysqli_error($connection));
}
// Fetch and display records
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
print($row["FirstName"] . " " . $row["LastName"] . " " . $row["PhoneNo"]);
print("
");
}
// Free result memory
mysqli_free_result($result);
// Close database connection
mysqli_close($connection);
?>
Through above mentioned example only those rows will be printed on the screen where FirstName will match with "Edge".
FAQ
1. What is the difference between WHERE and HAVING in MySQL?
The WHERE clause filters individual rows before grouping, whereas HAVING filters grouped results, usually after applying aggregate functions such as COUNT(), SUM() and AVG().
2. Can we use multiple WHERE conditions in a MySQL SELECT query?
Yes. You can combine multiple conditions using AND, OR and parentheses to control how the conditions are evaluated.
3. Why is the WHERE clause important in MySQL?
The WHERE clause helps retrieve only the required records instead of selecting every row from a database table. It makes database queries more targeted and useful for searching and filtering information.