(PHP MySQL) WHERE Clause


To select only data that matches a specified criteria, add a WHERE clause to the SELECT statement. The following example creates and populates a Customers table using the following SQL commands:

CREATE TABLE Customers (
   ID   INT AUTO_INCREMENT PRIMARY KEY,
   name VARCHAR(16) NOT NULL );

INSERT INTO Customers (name) VALUES ('Ben'), ('Bart'), ('Mandy'), ('Ella');

and selects customers using the BETWEEN ... AND operator:

SELECT * FROM Customers WHERE ID BETWEEN $low AND $high;

 <html><body>
 <?php
  $username = "user_id";
  $database = "user_id";
  $password = "p-word";
  $host     = "undcemmysql.mysql.database.azure.com";

  // Initializing MySQLi
  $conn     = mysqli_init( );

  // Creating an SSL connection
  mysqli_ssl_set( $conn, NULL, NULL, "DigiCertGlobalRootCA.crt.pem", NULL, NULL );

  // Opening a new connection to the MySQL server
  mysqli_real_connect( $conn, $host, $username, $password, $database, 3306 );

  // Connect to the database.
  if ( $conn->connect_errno )
    die( 'Failed to connect to MySQL: ' . $conn->connect_error );

  $action =  Drop    Create & populate  
             Select ( ≤ ID ≤ )    
  if ( $action == "Drop" ) {
    // Drop table Customers.
    $conn->query( "DROP TABLE Customers" );
    echo "Table Customers dropped";
  }
  elseif ( $action == "Create & populate" ) {
    // Create table Customers.
    $sql = "CREATE TABLE Customers (
              ID   INT AUTO_INCREMENT PRIMARY KEY,
              name VARCHAR(16) NOT NULL )";
    $conn->query( $sql );
    echo "Table Customers created";

    // Insert customers.
    $sql = "INSERT INTO Customers (name) VALUES
              ('Ben'), ('Bart'), ('Mandy'), ('Ella')";
    $conn->query( $sql );
    echo " Table Customers populated"
  }
  elseif ( $action == "Select" ) {
    // Select customers.
    $sql = "SELECT * FROM Customers WHERE ID BETWEEN $low AND $high";
    $result = $conn->query( $sql );
    while ( $row = $result->fetch_assoc( ) )
      echo $row['ID'] . " 🠞 " . $row['name'];
  }

  // Close the database connection.
  $conn->close( );
 ?>
 </body></html>