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.
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 = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully<br />'; $sql = "DROP TABLE tutorials_tbl"; mysql_select_db( 'TUTORIALS' ); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not delete table: ' . 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!