PHP MySQL Where
In this page:
SELECT * FROM table_name WHERE column = value;
SELECT * FROM table_name WHERE column > value AND other_column = value;
SELECT * FROM table_name WHERE column = value OR other_column = value;
Basic WHERE Filtering
Adding WHERE column = value to a SELECT statement restricts the results to only rows matching that condition -- SELECT * FROM users WHERE status = active returns only the active users, ignoring every other row in the table.
Example: Basic WHERE Filtering
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (name TEXT, status TEXT)");
$db->exec("INSERT INTO users VALUES ('Alice','active'), ('Bob','inactive')");
// Declare `$result`, set to `$db->query("SELECT * FROM users WHERE status = 'active'")`
$result = $db->query("SELECT * FROM users WHERE status = 'active'");
// Print a human-readable dump of `$result->fetchArray(SQLITE3_ASSOC)`
print_r($result->fetchArray(SQLITE3_ASSOC));
?>
Login to try C/C++/Java/PHP code in the editor
Comparison Operators in WHERE
Beyond exact equality, WHERE supports the full range of comparison operators -- >, <, >=, <=, and != -- for numeric and date comparisons, like finding every product with a price greater than 50, or every order placed after a certain date.
Example: Comparison Operators in WHERE
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE products (name TEXT, price INTEGER)");
$db->exec("INSERT INTO products VALUES ('Book', 60), ('Pen', 10)");
// Declare `$result`, set to `$db->query("SELECT * FROM products WHERE price > 50")`
$result = $db->query("SELECT * FROM products WHERE price > 50");
// Print a human-readable dump of `$result->fetchArray(SQLITE3_ASSOC)`
print_r($result->fetchArray(SQLITE3_ASSOC));
?>
Login to try C/C++/Java/PHP code in the editor
Combining Conditions with AND and OR
AND requires every listed condition to be true for a row to match, while OR requires at least one -- combining them (with parentheses for clarity) lets you build precise, multi-part filters, like "active users who are also over 18".
Example: Combining Conditions with AND and OR
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (name TEXT, status TEXT, age INTEGER)");
$db->exec("INSERT INTO users VALUES ('Alice','active',20)");
// Declare `$result`, set to `$db->query("SELECT * FROM users WHERE status = 'active' AND age > 18")`
$result = $db->query("SELECT * FROM users WHERE status = 'active' AND age > 18");
// Print a human-readable dump of `$result->fetchArray(SQLITE3_ASSOC)`
print_r($result->fetchArray(SQLITE3_ASSOC));
?>
Login to try C/C++/Java/PHP code in the editor
Pattern Matching with LIKE
LIKE matches text against a pattern using % as a wildcard for any number of characters -- WHERE name LIKE '%smith%' finds every name containing smith anywhere within it, which is useful for search-style filtering that an exact = comparison cannot do.
Example: Pattern Matching with LIKE
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (name TEXT)");
$db->exec("INSERT INTO users VALUES ('John Smith'), ('Jane Doe')");
// Declare `$result`, set to `$db->query("SELECT * FROM users WHERE name LIKE '%smith%'")`
$result = $db->query("SELECT * FROM users WHERE name LIKE '%smith%'");
// Print a human-readable dump of `$result->fetchArray(SQLITE3_ASSOC)`
print_r($result->fetchArray(SQLITE3_ASSOC));
?>
Login to try C/C++/Java/PHP code in the editor
Checking for NULL and Matching a List with IN
WHERE column IS NULL checks for a missing value (= NULL never matches, even against another NULL, due to SQL's three-valued logic), and WHERE column IN (v1, v2, v3) matches any row whose value is one of several listed options, compactly replacing a long chain of OR conditions.
Example: Checking for NULL and Matching a List with IN
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (name TEXT, phone TEXT)");
$db->exec("INSERT INTO users VALUES ('Alice', NULL), ('Bob', '555-1234')");
// Declare `$result`, set to `$db->query("SELECT * FROM users WHERE phone IS NULL")`
$result = $db->query("SELECT * FROM users WHERE phone IS NULL");
// Print a human-readable dump of `$result->fetchArray(SQLITE3_ASSOC)`
print_r($result->fetchArray(SQLITE3_ASSOC));
// Declare `$result2`, set to `$db->query("SELECT * FROM users WHERE name IN ('Alice', 'Carol')")`
$result2 = $db->query("SELECT * FROM users WHERE name IN ('Alice', 'Carol')");
// Print a human-readable dump of `$result2->fetchArray(SQLITE3_ASSOC)`
print_r($result2->fetchArray(SQLITE3_ASSOC));
?>
Login to try C/C++/Java/PHP code in the editor
- Building a WHERE clause by concatenating raw user input directly into the SQL string, opening the same SQL injection risk as an unsafe INSERT.
- Using a single = for an exact match when the intended comparison was actually a range or pattern match, like wanting contains behavior but using = instead of LIKE.
- Forgetting that string values in a WHERE clause need to be quoted in raw SQL, while numeric values do not -- though prepared statements avoid this distinction entirely.
- WHERE condition filters a query down to only the rows where that condition evaluates to true.
- Comparison operators (=, >, <, LIKE) and logical operators (AND, OR) combine to build precise filtering conditions.
- WHERE clause values from user input should always be bound through a prepared statement, never concatenated directly into the SQL string.
The WHERE clause is standard SQL and works identically across all MySQL and MariaDB versions PHP supports.
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- PHP MySQL Introduction
- PHP MySQLi Connection
- PHP PDO Introduction
- PHP CRUD Operations
- PHP Prepared Statements
- PHP Stored Procedures
- PHP Transactions
- PHP Error Handling in DB
- PHP MySQL Connect
- PHP MySQL Create DB
- PHP MySQL Create Table
- PHP MySQL Insert Data
- PHP MySQL Get Last ID
- PHP MySQL Insert Multiple
- PHP MySQL Prepared Statements
- PHP MySQL Select Data
- PHP MySQL Where
- PHP MySQL Order By
- PHP MySQL Delete Data
- PHP MySQL Update Data
- PHP MySQL Limit Data