Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

MySQL Where

PHP

MySQL WHERE Clause

WHERE clause MySQL में records को filter करने के लिए उपयोग किया जाता है। PHP में WHERE का उपयोग करके आप किसी भी specific condition के आधार पर data प्राप्त कर सकते हैं, जैसे कि class = 10 वाले students, या किसी particular city के users। यह SELECT, UPDATE, DELETE सभी queries का एक महत्वपूर्ण हिस्सा है।

  • WHERE का उपयोग specific data filter करने के लिए होता है।
  • Comparison operators जैसे =, >, <, !=, LIKE, BETWEEN आदि इसके साथ उपयोग किए जाते हैं।
  • Multiple conditions के लिए AND और OR का उपयोग किया जाता है।

Basic Syntax

SQL: WHERE clause
SELECT * FROM students WHERE class = 10;
-- class 10 वाले सभी students

SELECT * FROM students WHERE city = 'Delhi';
-- Delhi के students

SELECT * FROM students WHERE class = 10 AND city = 'Mumbai';
-- Multiple conditions

Important Notes

  • LIKE का उपयोग pattern matching के लिए किया जाता है।
  • AND multiple conditions को combine करता है।
  • OR loosely matched conditions को allow करता है।
  • NULL compare करने के लिए IS NULL और IS NOT NULL का उपयोग करें।

Operators with WHERE

Operator Description
= Equal to
> Greater than
< Less than
!= Not equal to
LIKE Pattern match
BETWEEN Range selection

MySQLi Procedural Example (WHERE)

यह example class = 10 वाले students को fetch करता है:

उदाहरण: MySQLi Procedural (WHERE)
 <?php
  $conn = mysqli_connect("localhost", "root", "", "school");
  $sql = "SELECT * FROM students WHERE class = 10";
  $result = mysqli_query($conn, $sql);
  while ($row = mysqli_fetch_assoc($result)) {
    echo $row['name'] . " - Class: " . $row['class'] . "<br>";
  }
  mysqli_close($conn);
  ?>

Multiple Conditions Example

AND का उपयोग करके multiple filters apply किए जा सकते हैं:

AND Condition Example
SELECT * FROM students WHERE class = 10 AND city = 'Delhi'

LIKE Example (Search Query)

LIKE का उपयोग Search bar inputs में बहुत होता है:

LIKE Example
SELECT * FROM students WHERE name LIKE 'A%'

// A से शुरू होने वाले नाम

PDO Example (WHERE)

PDO में WHERE clause prepared statements के साथ उपयोग करना recommended है:

उदाहरण: PDO (WHERE)
 <?php
  $pdo = new PDO("mysql:host=localhost;dbname=school", "root", "");
  $stmt = $pdo->prepare("SELECT * FROM students WHERE city = ?");
  $stmt->execute(['Delhi']);
  $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  foreach ($rows as $row) {
    echo $row['name'] . " - " . $row['city'] . "<br>";
  } ?>

PDO Example (LIKE)

LIKE Search (PDO)
  $search = "A%";
  $stmt = $pdo->prepare("SELECT * FROM students WHERE name LIKE ?");
  $stmt->execute([$search]);

Best Practices

  • User input हमेशा prepared statement के साथ उपयोग करें।
  • LIKE searches slow हो सकते हैं → indexing करें।
  • Paginated lists में WHERE + LIMIT दोनों उपयोग करें।
  • NULL values check करते समय IS NULL ही उपयोग करें, = NULL नहीं।
  • AND conditions पहले लगाएँ, फिर OR (execution fast)।

Common Mistakes & Fixes

गलती (Mistake) सही तरीका (Fix)
WHERE = NULL लिखना WHERE column IS NULL
LIKE '%name%' बिना index → slow Fulltext index या right-side wildcard use करें:
name LIKE 'A%'
User input direct query में डालना Always use prepared statements.

Conclusion

WHERE clause SQL queries का सबसे महत्वपूर्ण हिस्सा है जो आपको precise data filter करने में मदद करता है। PHP में इसे MySQLi और PDO दोनों में आसानी से उपयोग किया जा सकता है। सही तरीके से WHERE का उपयोग आपकी application की performance और accuracy दोनों को बेहतर बनाता है।

Article By: Brajlal Prasad
Created on: 05 Nov 2025  9  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]

Financial Calculator

Financial Calculator

Take control of your finances with our powerful Financial Calculator app—calculate loans, SIP, PPF, NPS and more all in one place!

Play Store