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

How to delete database table in php

藏色散人
藏色散人Original
2020-08-21 09:26:185003browse

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.

How to delete database table in php

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 = &#39;localhost&#39;;  // mysql服务器主机地址
$dbuser = &#39;root&#39;;            // mysql用户名
$dbpass = &#39;123456&#39;;          // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die(&#39;连接失败: &#39; . mysqli_error($conn));
}
echo &#39;连接成功<br />&#39;;
$sql = "DROP TABLE runoob_tbl";
mysqli_select_db( $conn, &#39;RUNOOB&#39; );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
  die(&#39;数据表删除失败: &#39; . 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!

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