HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

PHP MySQL Use The WHERE Clause

To select data from a MySQL database using the WHERE clause to filter results based on specific conditions.


Here's how you can use prepared statements with MySQLi and PDO:


These examples select data from the users table where the id column matches a specified condition (in this case, $user_id). Adjust the condition and column names as needed for your use case.

Using MySQLi:

<?php

// MySQLi Configuration
$servername = "localhost"; // Change this to your MySQL server address
$username = "username"; // Change this to your MySQL username
$password = "password"; // Change this to your MySQL password
$database = "dbname"; // Change this to your MySQL database name

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Condition
$user_id = 1; // Example condition

// SQL statement with WHERE clause
$sql = "SELECT * FROM users WHERE id = ?";

// Prepare the statement
$stmt = $conn->prepare($sql);

// Bind parameters to placeholders
$stmt->bind_param("i", $user_id); // "i" represents integer data type

// Execute the statement
$stmt->execute();

// Get result
$result = $stmt->get_result();

// Check if there are any rows returned
if ($result->num_rows > 0) {
    // Output data of each row
    while ($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "
"; } } else { echo "0 results"; } // Close statement and connection $stmt->close(); $conn->close(); ?>
You can click on above box to edit the code and run again.

Output

Using PDO :

<?php
// PDO Configuration
$servername = "localhost"; // Change this to your MySQL server address
$username = "username"; // Change this to your MySQL username
$password = "password"; // Change this to your MySQL password
$database = "dbname"; // Change this to your MySQL database name

try {
    // Connect to MySQL using PDO
    $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
    
    // Set PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Condition
    $user_id = 1; // Example condition
    
    // SQL statement with WHERE clause
    $sql = "SELECT * FROM users WHERE id = :id";
    
    // Prepare the statement
    $stmt = $conn->prepare($sql);
    
    // Bind values to named placeholders
    $stmt->bindParam(':id', $user_id);
    
    // Execute the statement
    $stmt->execute();
    
    // Fetch all rows as associative array
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    // Output data of each row
    foreach ($rows as $row) {
        echo "ID: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "
"; } } catch(PDOException $e) { echo "Error: " . $e->getMessage(); } // Close connection $conn = null; ?>
You can click on above box to edit the code and run again.

Output