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

WBOY
Release: 2023-09-14 21:18:02
forward
1051 people have browsed it

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);
?>
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template