If you have thousands of users in your database, selecting all of them at once isn't practical.
To filter records and extract only the data that fulfills specific conditions, you use the WHERE clause.
The WHERE clause is added to the end of a SELECT statement:
SELECT column_name(s) FROM table_name WHERE column_name operator value
<?php // Database connection code omitted for brevity...// Only select guests whose last name is 'Doe' $sql = "SELECT id, firstname, lastname FROM MyGuests WHERE lastname='Doe'"; $result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"] . " - Name: " . $row["firstname"] . " " . $row["lastname"] . "<br>"; } } else { echo "0 results"; } ?>
= : Equal> : Greater than< : Less thanLIKE : Search for a pattern (e.g., WHERE firstname LIKE '%Jo%')(Remember: Text values must be enclosed in single quotes, while numeric values should not be).