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.

PHP MySQL Where Clause

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); 

?> 

Conclusion

The WHERE Clause is useful to fetch the selected rows from a MySQL table.

I hope you find this article useful 🙂

Leave a Reply

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

Read previous post:
MySQL Select Data
PHP MySQL Select Data

PHP Select Data From MySQL allows selecting the data from the database. MySQL SELECT statement allows fetching zero or multiple...

Close