PHP MySQL Use WHERE

PHP MySQL Use WHERE – Complete Beginner Tutorial

The WHERE clause is one of the most important parts of MySQL queries. It allows you to filter records and retrieve or update only the data you need. Without WHERE, database operations can affect all records, which is often dangerous.

This tutorial explains how to use the WHERE clause in PHP MySQL with practical examples and outputs.


What Is WHERE Clause in PHP MySQL?

The WHERE clause is used to specify conditions in SQL queries. It filters records based on given criteria.

Basic SQL syntax:

SELECT * FROM table_name WHERE condition;

Only the rows that match the condition are affected.


Why WHERE Clause Is Important in PHP MySQL

The WHERE clause helps to:

  • Retrieve specific records
  • Update selected rows
  • Delete selected rows
  • Improve performance
  • Prevent accidental data changes

Almost every real-world PHP application uses WHERE conditions.


Prerequisites for Using WHERE in PHP

Before using WHERE, ensure you have:

  • PHP installed
  • MySQL database
  • A database connection file
  • A table with records

Sample MySQL Table Used in Examples

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100),
    age INT
);

PHP MySQL Database Connection Code

<?php
$conn = mysqli_connect("localhost", "root", "", "testdb");

if (!$conn) {
    die("Database connection failed");
}
?>

Select Data Using WHERE Clause in PHP MySQL

<?php
$query = "SELECT * FROM users WHERE id = 1";
$result = mysqli_query($conn, $query);

$row = mysqli_fetch_assoc($result);
echo $row['name'];
?>

Output:

Rahul

Using WHERE with String Values in PHP MySQL

<?php
$query = "SELECT * FROM users WHERE name = 'Amit'";
$result = mysqli_query($conn, $query);

while ($row = mysqli_fetch_assoc($result)) {
    echo $row['email'];
}
?>

Output:

amit@gmail.com

WHERE Clause with Comparison Operators

Common operators:

  • = equal
  • > greater than
  • < less than
  • >= greater than or equal
  • <= less than or equal

Example:

<?php
$query = "SELECT * FROM users WHERE age > 25";
$result = mysqli_query($conn, $query);

while ($row = mysqli_fetch_assoc($result)) {
    echo $row['name'] . "<br>";
}
?>

Output:

Amit

Using WHERE with AND Condition in PHP MySQL

<?php
$query = "SELECT * FROM users WHERE age > 20 AND name = 'Rahul'";
$result = mysqli_query($conn, $query);

while ($row = mysqli_fetch_assoc($result)) {
    echo $row['email'];
}
?>

Output:

rahul@example.com

Using WHERE with OR Condition in PHP MySQL

<?php
$query = "SELECT * FROM users WHERE name='Amit' OR name='Neha'";
$result = mysqli_query($conn, $query);

while ($row = mysqli_fetch_assoc($result)) {
    echo $row['name'] . "<br>";
}
?>

Output:

Amit
Neha

Using WHERE with LIKE Operator in PHP MySQL

<?php
$query = "SELECT * FROM users WHERE name LIKE 'R%'";
$result = mysqli_query($conn, $query);

while ($row = mysqli_fetch_assoc($result)) {
    echo $row['name'];
}
?>

Output:

Rahul

WHERE Clause with UPDATE Query in PHP

<?php
$query = "UPDATE users SET age = 27 WHERE name = 'Rahul'";
mysqli_query($conn, $query);

echo "Age updated";
?>

Output:

Age updated

WHERE Clause with DELETE Query in PHP

<?php
$query = "DELETE FROM users WHERE id = 3";
mysqli_query($conn, $query);

echo "Record deleted";
?>

Output:

Record deleted

Secure WHERE Clause Using Prepared Statements

Prepared statements protect against SQL injection.

<?php
$stmt = mysqli_prepare($conn, "SELECT * FROM users WHERE email = ?");
mysqli_stmt_bind_param($stmt, "s", $email);

$email = "rahul@example.com";
mysqli_stmt_execute($stmt);

$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);

echo $row['name'];
?>

Output:

Rahul

Common Mistakes When Using WHERE Clause

  • Forgetting quotes for string values
  • Using WHERE without index in large tables
  • Allowing unchecked user input
  • Using WHERE incorrectly with AND / OR

PHP MySQL WHERE, PHP MySQL filter data, PHP WHERE clause example, PHP MySQLi WHERE tutorial, PHP SELECT WHERE, PHP database condition, PHP MySQL query filtering


Best Practices for Using WHERE in PHP MySQL

  • Always validate user input
  • Use prepared statements
  • Use indexes on filtered columns
  • Test conditions before running UPDATE or DELETE
  • Avoid complex conditions when possible

You should Like this


Frequently Asked Questions (FAQ)

1. What does WHERE do in MySQL?

It filters records based on conditions.

2. Can WHERE be used with INSERT?

No, WHERE is not used with INSERT.

3. Is WHERE required for UPDATE?

Not required, but strongly recommended.

4. Can I use multiple WHERE conditions?

Yes, using AND / OR.

5. Is prepared WHERE safer?

Yes, it prevents SQL injection.

Related Article
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

PHP Tutorial – Complete Guide for Beginners to Advanced Welcome to the most comprehensive PHP tutorial available online at PHPOnline.in Read more

Introduction of PHP

Introduction to PHP – Learn PHP from Scratch with Practical Examples Welcome to your complete beginner's guide to PHP. Whether Read more

Syntax Overview of PHP

Syntax Overview of PHP (2025 Edition) Welcome to phponline.in, your one-stop platform for mastering PHP. This comprehensive, SEO-rich tutorial on Read more

Environment Setup in PHP

Setting Up PHP Environment (Beginner’s Guide) If you’re planning to learn PHP or start developing websites using PHP, the first Read more

Variable Types in PHP

PHP Variable Types: Complete Beginner's Guide to PHP Data Types Welcome to phponline.in, your trusted source for beginner-to-advanced level PHP Read more