How To Use SQL WHERE Clause With Example Using PHP

admin_img Posted By Bajarangi soft , Posted On 18-09-2020

WHERE keyword is used for fetching filtered data in a result set from the database.when we want to limit the query results to a specified condition. The SQL WHERE clause will handle that situations.

WHERE_Clause

 SQL WHERE Clause.

  • A SQL WHERE clause filters for rows to a particular criteria.

  • WHERE clause can be limited from the expression.

  • WHERE by condition that returns either true or false.

  • WHERE is used with SELECT, UPDATE, and DELETE.

 

SQL WHERE Syntax and Usage:

SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
 

Selecting the column name from the table where column name is equal to some value. 

Let's learn.

1.Create database and table
Table student
StudentID StudentName ParentsContact Address Country DOB Section

  1
    Liam 10101010 23 MG road
Banglore  
INDIA 12/06 A
  2    Andrew 20202020 2415 Anthony  market str Germany 01/02 A
  3    Joshua 30303030 26547 David church road USA 18/04 B
 4     Ryan 40404040 18 Krishna sq.
Banglore
INDIA 26/12 B
 5    Charles 50505050 878 Eli Berguvsvägen 8 INDIA 23/11 C


2.Create Sql where clause to select date of birth of Joshua from the table of student.
SELECT * FROM student WHERE DOB='18/04'"


3.Create index.php and implement code in PHP script .

$sql = "SELECT * FROM student WHERE DOB='18/04'";
..

if($data = mysqli_query($connect, $sql)){
    if(mysqli_num_rows($asc) > 0){
       ...
    }
}


4.List of Operators to use in SQL WHERE Clause.
Operator    Description
   =                Equal
   <>               Not equal.we can also written as !=
   >                Greater than
   <                Less than
   >=               Greater than or equal
   <=               Less than or equal
BETWEEN             Between an inclusive range
 LIKE               Search for a pattern
  IN                To specify multiple possible values for a column

5.Complete code of SQL Where Clause  Statement in PHP script.
<!DOCTYPE html>
<html lang="en">
<head>
    <title>How  can i use SQL WHERE Clause</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <br>
    <br>
    <br>
    <div class="text-center">
        <h1>SQL WHERE Clause</h1>
    </div>
    <div class="well">
        <?php
        $connect = mysqli_connect("localhost", "root", "", "school_management");

        if ($connect->connect_error) {
            die("Connection failed: " . $connect->connect_error);
        }
        $sql = "SELECT * FROM student WHERE DOB='18/04'";


        if($asc = mysqli_query($connect, $sql)){
            if(mysqli_num_rows($asc) > 0){
                echo "<table class='table'>
                    <tr>
                    <th>StudentName</th>
                    <th>DOB</th>
                    </tr>
                    ";
                while($row = $asc->fetch_assoc()){
                    echo "<tr>
                  <td>" . $row['StudentName'] . "</td>";
                    echo "<td>".$row['DOB']."</td>
                  </tr>";
                }
                echo "</table>";
            } else{
                echo "No records found.";
            }
        }
        ?>
    </div>
    <br>
</div>
</body>
</html>

Related Post