Home > Article > Backend Development > How to delete php database
Method to delete a php database: first create a PHP sample file; then connect to the database through "mysql_connect"; finally use SQL commands to mysql_query to delete a database.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to delete the php database?
Deleting a MySQL database using PHP
Deleting a database
If the database is no longer needed then it can be deleted permanently. You can delete a database using SQL commands passed to mysql_query.
Example
Try the following example to delete a database.
<?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'DROP DATABASE test_db'; $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not delete database db_test: ' . mysql_error()); } echo "Database deleted successfully\n"; mysql_close($conn); ?>
Warning: It is very dangerous to delete a database and tables. So before deleting any table or database you should make sure that everything you do is voluntary.
Delete a table
It issues a SQL command again through the mysql_query function to delete any database and data table. But be very careful when using this command because by doing so you can delete some important information on your desktop.
Try the following example to delete a table:
<?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'DROP TABLE employee'; $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not delete table employee: ' . mysql_error()); } echo "Table deleted successfully\n"; mysql_close($conn); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to delete php database. For more information, please follow other related articles on the PHP Chinese website!