How To Use SQL AND OR,NOT Operators In Statement In PHP

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

SQL operators which used to filtering the result set based on some condition. The WHERE clause can be combined with AND, OR, and NOT operators.

AND & OR NOT Operators

The SQL Logical  AND, OR and NOT Operators

  • AND operator veiw a data if two condition are TRUE.
  • OR operator veiw a data if one of two conditions is true.
  • NOT operator displays a data  if the condition(s) is NOT TRUE.
 

AND Syntax and Usage:

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;


OR Syntax and Usage:

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;


NOT Syntax and Usage:

SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

Lets Learn ,

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

Branch


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

Example of AND Operators 
$sql_1 = "SELECT * FROM Student WHERE Country='INDIA' AND Section='A'";

if($result_1 = mysqli_query($connect, $sql_1)){
    if(mysqli_num_rows($result_1) > 0){
...
}
}
In this statement  selecting only country and section A student.

Example of OR  Operators
$sql_2= "SELECT * FROM Student WHERE Section='A' OR Section='c'";

if($result_2 = mysqli_query($connect, $sql_2)){
    if(mysqli_num_rows($result_2) > 0){
...
}
}
In this statement  selecting  country section A or section C students.

 Example of NOT Operators
$sql_3 = "SELECT * FROM Student WHERE NOT Country='INDIA'";

if($result_3 = mysqli_query($connect, $sql_3)){
    if(mysqli_num_rows($result_3) > 0){
...
}
}

In this statement  selecting  country which is not india.

5.Complete code of AND, OR and NOT Operators in PHP script.
<!DOCTYPE html>
<html lang="en">
<head>
    <title>How to use SQL AND & OR,NOT Operators</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 AND & OR,NOT Operators</h1>
    </div>
    <div class="well">
        <?php
        $connect = mysqli_connect("localhost", "root", "", "school_management");

        if ($connect->connect_error) {
            die("Connection failed: " . $connect->connect_error);
        }
        $sql_1 = "SELECT * FROM Student
            WHERE Country='INDIA' AND  Section='A'";
        $sql_2= "SELECT * FROM Student WHERE Section='A' OR Section='c'";
        $sql_3 = "SELECT * FROM Student WHERE NOT Country='INDIA'";

        if($result_1 = mysqli_query($connect, $sql_1)){
            if(mysqli_num_rows($result_1) > 0){
                echo "<table class='table'>
                    <tr>
                    <th>StudentID</th>
                    <th>StudentName</th>
                    <th>ParentsContact</th>
                    <th>Address</th>
                      <th>Country</th>
                    <th>DOB</th>
                    <th>Section</th>
                    <th>Branch</th>
                    </tr>
                    ";
                while($row = $result_1->fetch_assoc()){
                    echo "<tr>
                  <td>" . $row['StudentID'] . "</td>
                  <td>".$row['StudentName'] ."</td>
                  <td>".$row['ParentsContact'] ."</td>
                  <td>".$row['Address'] ."</td>
                  <td>".$row['Country'] ."</td>
                  <td>".$row['DOB'] ."</td>
                  <td>".$row['Section'] ."</td>
                  <td>".$row['Branch'] ."</td>
                  </tr>";
                }
                echo "</table>";
            } else{
                echo "No records found.";
            }
        }

            echo"<br>";
        if($result_2 = mysqli_query($connect, $sql_2)){
            if(mysqli_num_rows($result_2) > 0){
                echo "<table class='table'>
                    <tr>
                    <th>StudentID</th>
                    <th>StudentName</th>
                    <th>ParentsContact</th>
                    <th>Address</th>
                      <th>Country</th>
                    <th>DOB</th>
                    <th>Section</th>
                    <th>Branch</th>
                    </tr>
                    ";
                while($row = $result_2->fetch_assoc()){
                    echo "<tr>
                  <td>" . $row['StudentID'] . "</td>
                  <td>".$row['StudentName'] ."</td>
                  <td>".$row['ParentsContact'] ."</td>
                  <td>".$row['Address'] ."</td>
                  <td>".$row['Country'] ."</td>
                  <td>".$row['DOB'] ."</td>
                  <td>".$row['Section'] ."</td>
                  <td>".$row['Branch'] ."</td>
                  </tr>";
                }
                echo "</table>";
            } else{
                echo "No records found.";
            }
        }

        echo"<br>";

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

Related Post