MySQL Where

PHP MySQL The WHERE Clause

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.


Filtering Data

The WHERE clause is added to the end of a SELECT statement: SELECT column_name(s) FROM table_name WHERE column_name operator value

WHERE Clause Example

<?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"; } ?>

Common Operators in WHERE:

(Remember: Text values must be enclosed in single quotes, while numeric values should not be).