SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
The WHERE
clause in SQL is used to filter records based on specific conditions. When interfacing with a MySQL database using PHP, you can use the WHERE
clause to select specific records, update records, or delete records.
Here's how you can use the WHERE
clause with PHP and MySQL:
Suppose you want to fetch records from a table named users
where the username
is "john_doe":
<?php $servername = "localhost"; $username = "your_username"; $password = "your_password"; $dbname = "your_db_name"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT id, username, email FROM users WHERE username='john_doe'"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo "ID: " . $row["id"] . " - Name: " . $row["username"] . " - Email: " . $row["email"] . "<br>"; } } else { echo "0 results"; } $conn->close(); ?>
To update the email address of a user with the username "john_doe":
$sql = "UPDATE users SET email='new_email@example.com' WHERE username='john_doe'"; if ($conn->query($sql) === TRUE) { echo "Record updated successfully"; } else { echo "Error updating record: " . $conn->error; }
To delete the user with the username "john_doe":
$sql = "DELETE FROM users WHERE username='john_doe'"; if ($conn->query($sql) === TRUE) { echo "Record deleted successfully"; } else { echo "Error deleting record: " . $conn->error; }
When working with user input or variable data, it's crucial to use prepared statements to avoid SQL injection. Here's how you can use the WHERE
clause with prepared statements:
$stmt = $conn->prepare("SELECT id, username, email FROM users WHERE username=?"); $stmt->bind_param("s", $username); // "s" denotes a string $username = "john_doe"; $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { echo "ID: " . $row["id"] . " - Name: " . $row["username"] . " - Email: " . $row["email"] . "<br>"; } $stmt->close();
This method is more secure as the user input is never directly used in the SQL statement, making SQL injections much harder.
PHP MySQL WHERE clause examples:
$conn = new mysqli("localhost", "your_username", "your_password", "your_database"); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $condition = "your_condition"; // Replace with your actual condition $sql = "SELECT * FROM your_table WHERE $condition"; $result = $conn->query($sql); // Process the result set... $conn->close();
How to use WHERE clause in PHP and MySQL:
$conn = new mysqli("localhost", "your_username", "your_password", "your_database"); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $condition = "your_condition"; // Replace with your actual condition $sql = "SELECT * FROM your_table WHERE $condition"; $result = $conn->query($sql); // Process the result set... $conn->close();
Filtering data with WHERE clause in PHP:
$status = "active"; // Example condition $sql = "SELECT * FROM your_table WHERE status = '$status'";
Using variables in MySQL WHERE clause with PHP:
$condition = "your_condition"; // Replace with your actual condition $sql = "SELECT * FROM your_table WHERE $condition";
PHP MySQL WHERE clause multiple conditions:
$status = "active"; $category = "electronics"; $sql = "SELECT * FROM your_table WHERE status = '$status' AND category = '$category'";
Dynamic SQL queries with WHERE clause in PHP:
$condition = ""; // Construct condition dynamically based on user input or logic $sql = "SELECT * FROM your_table WHERE $condition";
PHP MySQL WHERE clause for string matching:
$keyword = "example"; $sql = "SELECT * FROM your_table WHERE column_name LIKE '%$keyword%'";
BETWEEN and IN operators in WHERE clause with PHP:
// BETWEEN operator $start_date = "2023-01-01"; $end_date = "2023-12-31"; $sql = "SELECT * FROM your_table WHERE date_column BETWEEN '$start_date' AND '$end_date'"; // IN operator $categories = ["electronics", "clothing", "books"]; $category_string = implode("','", $categories); $sql = "SELECT * FROM your_table WHERE category IN ('$category_string')";
Handling NULL values in WHERE clause with PHP:
$sql = "SELECT * FROM your_table WHERE column_name IS NULL";
PHP MySQL WHERE clause date conditions:
$start_date = "2023-01-01"; $end_date = "2023-12-31"; $sql = "SELECT * FROM your_table WHERE date_column BETWEEN '$start_date' AND '$end_date'";
Escaping user input in WHERE clause with PHP:
$user_input = mysqli_real_escape_string($conn, $_POST['user_input']); $sql = "SELECT * FROM your_table WHERE column_name = '$user_input'";
SQL LIKE operator in WHERE clause with PHP:
$keyword = "example"; $sql = "SELECT * FROM your_table WHERE column_name LIKE '%$keyword%'";
Combining logical operators in MySQL WHERE clause with PHP:
$status = "active"; $category = "electronics"; $sql = "SELECT * FROM your_table WHERE status = '$status' OR category = '$category'";