MySQLi Tutorial on MySQLi Delete Query

if you want to delete a record from any mysql table, then you can use the sql command delete from. you can use this command at the mysql> prompt as well as in any script like php.

syntax

the following code block has a generic sql syntax of the delete command to delete data from a mysql table.

delete from table_name [where clause]
  • if the where clause is not specified, then all the records will be deleted from the given mysql table.

  • you can specify any condition using the where clause.

  • you can delete records in a single table at a time.

the where clause is very useful when you want to delete selected rows in a table.

deleting data from the command prompt

this will use the sql delete command with the where clause to delete selected data into the mysql table – tutorials_tbl.

example

the following example will delete a record from the tutorial_tbl whose tutorial_id is 3.

root@host# mysql -u root -p password;
enter password:*******

mysql> use tutorials;
database changed

mysql> delete from tutorials_tbl where tutorial_id=3;
query ok, 1 row affected (0.23 sec)

mysql>

deleting data using a php script

php uses mysqli query() or mysql_query() function to delete records in a mysql table. this function takes two parameters and returns true on success or false on failure.

syntax

$mysqli→query($sql,$resultmode)

sr.no. parameter & description
1

$sql

required - sql query to delete records in a mysql table.

2

$resultmode

optional - either the constant mysqli_use_result or mysqli_store_result depending on the desired behavior. by default, mysqli_store_result is used.

example

try the following example to delete a record in a table −

copy and paste the following example as mysql_example.php −

<html>
   <head>
      <title>deleting mysql table record</title>
   </head>
   <body>
      <?php
         $dbhost = 'localhost';
         $dbuser = 'root';
         $dbpass = 'root@123';
         $dbname = 'tutorials';
         $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
         
         if($mysqli→connect_errno ) {
            printf("connect failed: %s<br />", $mysqli→connect_error);
            exit();
         }
         printf('connected successfully.<br />');
		 
         if ($mysqli→query('delete from tutorials_tbl where tutorial_id = 4')) {
            printf("table tutorials_tbl record deleted successfully.<br />");
         }
         if ($mysqli→errno) {
            printf("could not delete record from table: %s<br />", $mysqli→error);
         }
   
         $sql = "select tutorial_id, tutorial_title, tutorial_author, submission_date from tutorials_tbl";
		 
         $result = $mysqli→query($sql);
           
         if ($result→num_rows > 0) {
            while($row = $result→fetch_assoc()) {
               printf("id: %s, title: %s, author: %s, date: %d <br />", 
                  $row["tutorial_id"], 
                  $row["tutorial_title"], 
                  $row["tutorial_author"],
                  $row["submission_date"]);               
            }
         } else {
            printf('no record found.<br />');
         }
         mysqli_free_result($result);
         $mysqli→close();
      ?>
   </body>
</html>

output

access the mysql_example.php deployed on apache web server and verify the output. here we've entered multiple records in the table before running the select script.

connected successfully.
table tutorials_tbl record deleted successfully.
id: 1, title: mysql tutorial, author: mahesh, date: 2021
id: 2, title: html tutorial, author: mahesh, date: 2021
id: 3, title: php tutorial, author: mahesh, date: 2021
id: 5, title: apache tutorial, author: suresh, date: 2021