PHP MySQL Where Clause

The MySQL WHERE clause is useful to filter only those records that are fulfilled by the user’s specific condition. The SQL WHERE Clause is useful to restrict the number of rows affected by a SELECT, UPDATE, or DELETE query.

Where Clause in MySQL

The WHERE clause works like an if condition in any programming language. The WHERE clause is useful to compare the given value with the field value available in a MySQL table. If the given value from outside is equal to the available field value in the MySQL table, it returns that row.

Syntax

SELECT Column1 , Column2 , ...

FROM Table_Name

WHERE Condition

Read Also: PHP MySQL Select Data

WHERE Clause Using Procedural Method

<?php

$conn = mysqli_connect("localhost", "user_name", "password", "database");

if($conn === false){ 

    die("ERROR: Could not connect. " . mysqli_connect_error()); 



$query = "SELECT * FROM Data WHERE Firstname='ram'"; 

if($result = mysqli_query($conn, $query)){ 

    if(mysqli_num_rows($result) > 0){ 

        echo "<table>"; 

            echo "<tr>"; 

                echo "<th>Firstname</th>"; 

                echo "<th>Lastname</th>"; 

                echo "<th>Age</th>"; 

            echo "</tr>"; 

        while($row = mysqli_fetch_array($result)){ 

            echo "<tr>"; 

                echo "<td>" . $row['Firstname'] . "</td>"; 

                echo "<td>" . $row['Lastname'] . "</td>"; 

                echo "<td>" . $row['Age'] . "</td>"; 

            echo "</tr>"; 

        } 

        echo "</table>"; 

        mysqli_free_result($result); 

    } else{ 

        echo "No Matching records are found."; 

    } 

} else{ 

    echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn); 



mysqli_close($conn); 

?>

WHERE Clause Using Object-Oriented Method

<?php

$mysqli = new mysqli("localhost", "user_name", "password", "database"); 

if($mysqli === false){ 

    die("ERROR: Could not connect. " . $mysqli->connect_error); 



$query = "SELECT * FROM Data WHERE Firstname='ram'"; 

if($result = $mysqli->query($query)){ 

    if($result->num_rows > 0){ 

        echo "<table>"; 

            echo "<tr>"; 

                echo "<th>Firstname</th>"; 

                echo "<th>Lastname</th>"; 

                echo "<th>Age</th>"; 

            echo "</tr>"; 

        while($row = $result->fetch_array()){ 

            echo "<tr>"; 

                echo "<td>" . $row['Firstname'] . "</td>"; 

                echo "<td>" . $row['Lastname'] . "</td>"; 

                echo "<td>" . $row['Age'] . "</td>"; 

            echo "</tr>"; 

        } 

        echo "</table>"; 

        $result->free(); 

    } else{ 

        echo "No matching records are found."; 

    } 

} else{ 

    echo "ERROR: Could not able to execute $query. " . $mysqli->error; 



$mysqli->close(); 

?> 

WHERE Clause Using PDO Method

<?php

try{ 

    $pdo = new PDO("mysql:host=localhost; dbname=database", "user_name", "password"); 

    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

} catch(PDOException $e) { 

    die("ERROR: Could not connect. "  . $e->getMessage()); 

}

try{ 

    $query = "SELECT * FROM Data WHERE Firstname='ram'"; 

    $result = $pdo->query($query); 

    if($result->rowCount() > 0){ 

        echo "<table>"; 

            echo "<tr>"; 

                echo "<th>Firstname</th>"; 

                echo "<th>Lastname</th>"; 

                echo "<th>Age</th>"; 

            echo "</tr>"; 

        while($row = $res->fetch()){ 

            echo "<tr>"; 

                echo "<td>" . $row['Firstname'] . "</td>"; 

                echo "<td>" . $row['Lastname'] . "</td>"; 

                echo "<td>" . $row['Age'] . "</td>"; 

            echo "</tr>"; 

        } 

        echo "</table>"; 

        unset($res); 

    } else{ 

        echo "No records matching are found."; 

    } 

} catch(PDOException $e) { 

    die("ERROR: Could not able to execute $sql. "  . $e->getMessage()); 



unset($pdo); 

?> 

Read More: PHP MySQL Delete Data

Conclusion

The WHERE clause is useful to fetch the selected rows according to the specifications mentioned by the user. It is a useful tool to efficiently work with databases. 

In case, you want to learn about how to connect to MySQL using PHP and creating databases, do check out our previous blogs. Happy Learning 🙂

One thought on “PHP MySQL Where Clause

Leave a Reply

Your email address will not be published. Required fields are marked *