MySQL Where
WHERE filters database rows according to a condition.
What is it?
WHERE filters database rows according to a condition.
Why do we use it?
It allows applications to retrieve only the records they need.
How does it work?
MySQL evaluates the WHERE condition and returns matching rows.
Syntax
SELECT * FROM students WHERE id=?;
Example
<?php
$conn = new mysqli("localhost", "root", "", "aifa_php_lab");
$id = 1;
$stmt = $conn->prepare("SELECT id,name,email FROM students WHERE id=?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo htmlspecialchars($row['name']);
}
?>
Example Explained
More Examples
Use WHERE with email, course, city or status.
⚠️ Important Rules
Use prepared statements when filter values come from users.
❌ Common Mistakes
Forgetting WHERE when only one record is intended.
🌍 Real-World Use
Search, login, profiles and admin filtering.
📝 Practice Exercise
Create a student search by ID and email.
💻 Try It Yourself
Write and run this PHP lesson inside the AIFA Coding Lab.