Table of Contents:
PHP MySQL Use WHERE – Complete Beginner Tutorial
The WHERE clause is one of the most important parts of MySQL queries. It allows you to filter records and retrieve or update only the data you need. Without WHERE, database operations can affect all records, which is often dangerous.
This tutorial explains how to use the WHERE clause in PHP MySQL with practical examples and outputs.
What Is WHERE Clause in PHP MySQL?
The WHERE clause is used to specify conditions in SQL queries. It filters records based on given criteria.
Basic SQL syntax:
SELECT * FROM table_name WHERE condition;
Only the rows that match the condition are affected.
Why WHERE Clause Is Important in PHP MySQL
The WHERE clause helps to:
- Retrieve specific records
- Update selected rows
- Delete selected rows
- Improve performance
- Prevent accidental data changes
Almost every real-world PHP application uses WHERE conditions.
Prerequisites for Using WHERE in PHP
Before using WHERE, ensure you have:
- PHP installed
- MySQL database
- A database connection file
- A table with records
Sample MySQL Table Used in Examples
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
age INT
);
PHP MySQL Database Connection Code
<?php
$conn = mysqli_connect("localhost", "root", "", "testdb");
if (!$conn) {
die("Database connection failed");
}
?>
Select Data Using WHERE Clause in PHP MySQL
<?php
$query = "SELECT * FROM users WHERE id = 1";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
echo $row['name'];
?>
Output:
Rahul
Using WHERE with String Values in PHP MySQL
<?php
$query = "SELECT * FROM users WHERE name = 'Amit'";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['email'];
}
?>
Output:
amit@gmail.com
WHERE Clause with Comparison Operators
Common operators:
=equal>greater than<less than>=greater than or equal<=less than or equal
Example:
<?php
$query = "SELECT * FROM users WHERE age > 25";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['name'] . "<br>";
}
?>
Output:
Amit
Using WHERE with AND Condition in PHP MySQL
<?php
$query = "SELECT * FROM users WHERE age > 20 AND name = 'Rahul'";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['email'];
}
?>
Output:
rahul@example.com
Using WHERE with OR Condition in PHP MySQL
<?php
$query = "SELECT * FROM users WHERE name='Amit' OR name='Neha'";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['name'] . "<br>";
}
?>
Output:
Amit
Neha
Using WHERE with LIKE Operator in PHP MySQL
<?php
$query = "SELECT * FROM users WHERE name LIKE 'R%'";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['name'];
}
?>
Output:
Rahul
WHERE Clause with UPDATE Query in PHP
<?php
$query = "UPDATE users SET age = 27 WHERE name = 'Rahul'";
mysqli_query($conn, $query);
echo "Age updated";
?>
Output:
Age updated
WHERE Clause with DELETE Query in PHP
<?php
$query = "DELETE FROM users WHERE id = 3";
mysqli_query($conn, $query);
echo "Record deleted";
?>
Output:
Record deleted
Secure WHERE Clause Using Prepared Statements
Prepared statements protect against SQL injection.
<?php
$stmt = mysqli_prepare($conn, "SELECT * FROM users WHERE email = ?");
mysqli_stmt_bind_param($stmt, "s", $email);
$email = "rahul@example.com";
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);
echo $row['name'];
?>
Output:
Rahul
Common Mistakes When Using WHERE Clause
- Forgetting quotes for string values
- Using WHERE without index in large tables
- Allowing unchecked user input
- Using WHERE incorrectly with AND / OR
PHP MySQL WHERE, PHP MySQL filter data, PHP WHERE clause example, PHP MySQLi WHERE tutorial, PHP SELECT WHERE, PHP database condition, PHP MySQL query filtering
Best Practices for Using WHERE in PHP MySQL
- Always validate user input
- Use prepared statements
- Use indexes on filtered columns
- Test conditions before running UPDATE or DELETE
- Avoid complex conditions when possible
You should Like this
Frequently Asked Questions (FAQ)
1. What does WHERE do in MySQL?
It filters records based on conditions.
2. Can WHERE be used with INSERT?
No, WHERE is not used with INSERT.
3. Is WHERE required for UPDATE?
Not required, but strongly recommended.
4. Can I use multiple WHERE conditions?
Yes, using AND / OR.
5. Is prepared WHERE safer?
Yes, it prevents SQL injection.