Home  >  Article  >  Backend Development  >  How to write a PHP script to delete data from an existing MySQL table?

How to write a PHP script to delete data from an existing MySQL table?

WBOY
WBOYforward
2023-09-14 21:17:031061browse

How to write a PHP script to delete data from an existing MySQL table?

We can use SQL DELETE command with or without WHERE clause in PHP function mysql_query(). This function will execute a SQL command in a manner similar to that executed at the mysql> prompt. To illustrate this, we have the following example -

Example

In this example, we are writing a PHP script to delete a record from a MySQL table named 'tutorial_tbl' whose tutorial_id is 3.

<?php
   $dbhost = 'localhost:3036';
   $dbuser = 'root';
   $dbpass = 'rootpassword';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);

   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   }

   $sql = 'DELETE FROM tutorials_tbl WHERE tutorial_id=3';

   mysql_select_db('TUTORIALS');
   $retval = mysql_query( $sql, $conn );

   if(! $retval ) {
      die('Could not delete data: ' . mysql_error());
   }
   echo "Deleted data successfully</p><p>";
   mysql_close($conn);
?>

The above is the detailed content of How to write a PHP script to delete data from an existing MySQL table?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete