Home  >  Article  >  Backend Development  >  How to delete mysql table in php

How to delete mysql table in php

藏色散人
藏色散人Original
2021-06-10 09:12:202237browse

How to delete a mysql table in php: first connect to the database through the "mysql_connect" function; then use the "mysql_query()" function to delete the table in the database.

How to delete mysql table in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to delete the mysql table with php?

  • MySQL Delete Table

Deleting an existing MySQL table is easy, but you have to Be very careful, because if you delete the table, you cannot recover the data.

Syntax

The common SQL commands to delete MySQL tables are:

DROP TABLE table_name ;

Delete tables through the command line

Just use DROP TABLE on the command line This SQL command will do.

Example

In the following example, the table tutorials_tbl is deleted.

root@host# mysql -u root -p
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> DROP TABLE tutorials_tbl
Query OK, 0 rows affected (0.8 sec)
mysql>
  • Use PHP script to delete tables

To use PHP script to delete tables in the database, you need to use the function mysql_query(). The table can be deleted by passing the correct SQL command into the second parameter of the function.

Example

<html>
<head>
<title>Creating MySQL Tables</title>
</head>
<body>
<?php
$dbhost = &#39;localhost:3036&#39;;
$dbuser = &#39;root&#39;;
$dbpass = &#39;rootpassword&#39;;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die(&#39;Could not connect: &#39; . mysql_error());
}
echo &#39;Connected successfully<br />&#39;;
$sql = "DROP TABLE tutorials_tbl";
mysql_select_db( &#39;TUTORIALS&#39; );
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die(&#39;Could not delete table: &#39; . mysql_error());
}
echo "Table deleted successfully\n";
mysql_close($conn);
?>
</body>
</html>

[Recommended learning: PHP video tutorial]

The above is the detailed content of How to delete mysql table in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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