PHP MYSQL Database

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 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 🙂

Sahil Jani

Share
Published by
Sahil Jani

Recent Posts

5 Important Things To Know About WordPress Before You Use It

There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…

3 years ago

How to Install MySQL on Your PC in 3 Easy Steps

In this tutorial, I'm going to show you how to install MySQL on your computer.…

4 years ago

Download and Install Turbo C++ for Windows 10 (Full Installation Guide)

Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…

5 years ago

PHP .HTACCESS Redirects

We can redirect any webpage to any other or redirect the whole domain or website…

5 years ago

PHP Redirect Pages

There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…

5 years ago

PHP Include & Required

Include files in PHP are used in appending various global or config files. We can…

5 years ago