(PHP MySQL) DELETE FROM


The DELETE FROM statement is used to delete rows from a database table. The following example creates and populates a Members table using the following SQL commands:

CREATE TABLE Members (
  ID   INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(16) NOT NULL );
   
INSERT INTO Members( name ) VALUES( '$name1' );

and deletes the items using:

DELETE FROM Members WHERE name LIKE '%$name2%';


 <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 table    Create table    Select all      
              Insert ( name = "" )                    
              Delete ( WHERE name LIKE '')            
  if ( $action == "Drop" ) {
    // Drop table Members.
    $conn->query( "DROP TABLE Members" );
    echo "Table Members dropped";
  }
  elseif ( $action == "Create" ) {
    // Create table Members.
    $sql = "CREATE TABLE Members (
              ID   INT AUTO_INCREMENT PRIMARY KEY,
              name VARCHAR(16) NOT NULL )";
    $conn->query( $sql );
    echo "Table Members created";
  }
  elseif ( $action == "Select all" ) {
    // Select all members.
    $result = $conn->query( "SELECT * FROM Members" );
    while ( $row = $result->fetch_assoc( ) )
      echo $row['ID'] . " 🠞 " . $row['name'];
  }
  elseif ( $action == "Insert" ) {
    // Insert member.
    $sql = "INSERT INTO Members( name ) VALUES( '$name1' )";
    $conn->query( $sql );
    echo "Member $name1 entered"
  }
  elseif ( $action == "Delete" ) {
    // Delete members.
    $sql = "DELETE FROM Members WHERE name LIKE '%$name2%'";
    $conn->query( $sql );
    // Select all members.
    $result = $conn->query( "SELECT * FROM Members" );
    while ( $row = $result->fetch_assoc( ) )
      echo $row['ID'] . " 🠞 " . $row['name'];
  }

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




      Two fish are sitting in a tank.    
      One looks over at the other and says:    
      “Hey, do you know how to drive this thing?”