← Back to PHP Course | Chapter 11: Database | Lesson 17 of 21

PHP MySQL Where

किसी table की हर row select करना शायद ही कभी उपयोगी होता है एक बार उस table में कुछ rows से ज़्यादा हो जाएँ -- आप लगभग हमेशा सिर्फ किसी condition से match करने वाली rows चाहते हैं, जैसे किसी specific user की ID, या $50 से कम हर product। WHERE clause एक SELECT (या UPDATE, या DELETE) statement को exactly match करने वाली rows तक filter करता है।
Syntax
php
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

किसी SELECT statement में WHERE column = value add करना results को सिर्फ उस condition से match करने वाली rows तक restrict करता है -- SELECT * FROM users WHERE status = active सिर्फ active users return करता है, table की बाकी हर row ignore करते हुए।

उदाहरण: Basic WHERE Filtering

php
<?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));
?>

WHERE में Comparison Operators

exact equality से आगे, WHERE comparison operators की पूरी range support करता है -- >, <, >=, <=, और != -- numeric और date comparisons के लिए, जैसे 50 से ज़्यादा price वाला हर product ढूँढना, या किसी certain date के बाद placed हर order।

उदाहरण: Comparison Operators in WHERE

php
<?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));
?>

AND और OR से Conditions Combine करना

AND माँग करता है कि किसी row के match होने के लिए listed हर condition true हो, जबकि OR कम से कम एक की माँग करता है -- उन्हें combine करना (clarity के लिए parentheses के साथ) आपको precise, multi-part filters बनाने देता है, जैसे "active users who are also over 18"।

उदाहरण: Combining Conditions with AND and OR

php
<?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));
?>

LIKE से Pattern Matching

LIKE किसी भी संख्या में characters के लिए wildcard की तरह % इस्तेमाल करके text को एक pattern के against match करता है -- WHERE name LIKE '%smith%' कहीं भी smith रखने वाला हर name ढूँढता है, जो search-style filtering के लिए उपयोगी है जो एक exact = comparison नहीं कर सकता।

उदाहरण: Pattern Matching with LIKE

php
<?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));
?>

NULL Check करना और IN से एक List Match करना

WHERE column IS NULL एक missing value check करता है (= NULL कभी match नहीं करता, यहाँ तक कि किसी दूसरे NULL के against भी नहीं, SQL की three-valued logic की वजह से), और WHERE column IN (v1, v2, v3) किसी भी ऐसी row को match करता है जिसकी value listed कई options में से एक हो, OR conditions की एक लंबी chain को compactly replace करते हुए।

उदाहरण: Checking for NULL and Matching a List with IN

php
<?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));
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. raw user input को सीधे SQL string में concatenate करके एक WHERE clause बनाना, एक unsafe INSERT जैसा ही SQL injection risk खोलते हुए।
  2. exact match के लिए एक single = इस्तेमाल करना जब intended comparison actually एक range या pattern match था, जैसे contains behavior चाहना लेकिन = के बजाय LIKE की ज़रूरत हो।
  3. यह भूल जाना कि raw SQL में एक WHERE clause में string values को quote करना ज़रूरी है, जबकि numeric values को नहीं -- हालाँकि prepared statements इस distinction से पूरी तरह बचाते हैं।
चैप्टर सारांश
  • WHERE condition एक query को सिर्फ उन rows तक filter करता है जहाँ वह condition true evaluate होती है।
  • Comparison operators (=, >, <, LIKE) और logical operators (AND, OR) मिलकर precise filtering conditions बनाते हैं।
  • user input से आई WHERE clause values को हमेशा एक prepared statement के through bound किया जाना चाहिए, कभी सीधे SQL string में concatenated नहीं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.