php method to delete a database table: first create a PHP sample file; then connect to the mysql database; finally delete the MySQL data table through the "DROP TABLE runoob_tbl" statement.
Recommended: "PHP Video Tutorial"
php MySQL delete data table
Deleting a data table in MySQL is very easy, but you must be very careful when deleting a table because all data will disappear after executing the delete command.
Syntax
The following is the general syntax for deleting MySQL data tables:
DROP TABLE table_name ;
Delete the data table in the command prompt window
In the mysql> command prompt window The SQL statement to delete the data table is DROP TABLE:
Example
The following example deletes the data table runoob_tbl:
root@host# mysql -u root -p Enter password:******* mysql> use RUNOOB; Database changed mysql> DROP TABLE runoob_tbl Query OK, 0 rows affected (0.8 sec) mysql>
Use PHP script to delete the data table
PHP uses the mysqli_query function to delete MySQL data tables.
This function has two parameters and returns TRUE when executed successfully, otherwise it returns FALSE.
Syntax
mysqli_query(connection,query,resultmode);
Parameters
connection required. Specifies the MySQL connection to use.
query Required, specifies the query string.
resultmode
Optional. a constant. Can be any of the following values:
MYSQLI_USE_RESULT (use this if you need to retrieve large amounts of data)
MYSQLI_STORE_RESULT (default)
Example
The following example uses a PHP script to delete the data table runoob_tbl:
Delete database
<?php $dbhost = 'localhost'; // mysql服务器主机地址 $dbuser = 'root'; // mysql用户名 $dbpass = '123456'; // mysql用户名密码 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('连接失败: ' . mysqli_error($conn)); } echo '连接成功<br />'; $sql = "DROP TABLE runoob_tbl"; mysqli_select_db( $conn, 'RUNOOB' ); $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die('数据表删除失败: ' . mysqli_error($conn)); } echo "数据表删除成功\n"; mysqli_close($conn); ?>
After successful execution, we use the following command and the runoob_tbl table will no longer be visible:
mysql> show tables; Empty set (0.01 sec)
The above is the detailed content of How to delete database table in php. For more information, please follow other related articles on the PHP Chinese website!