Latest web development tutorials

Delete MySQL database

Delete the database using mysqladmin

Use ordinary user login mysql server, you may need special permission to create or delete MySQL database.

So here we are logged in as root, the root user has the highest authority, you can use the mysql mysqladmin command to create the database.

In the process of deleting the database, it is important to be very careful, because after the delete command, all data will be lost.

The following examples delete the database w3big (the database has been created in the previous section):

[root@host]# mysqladmin -u root -p drop w3big
Enter password:******

After executing the above command to delete the database, there will be a prompt to confirm whether it delete the database:

Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.

Do you really want to drop the 'w3big' database [y/N] y
Database "w3big" dropped

Use PHP script to delete database

PHP use mysql_query function to create or delete MySQL database.

This function has two parameters, in the implementation of successful returns TRUE, otherwise returns FALSE.

grammar

bool mysql_query( sql, connection );
parameter description
sql Required. SQL query to send provisions. Note: The query string should not end with a semicolon.
connection Optional. Provisions of SQL connection identifier. If not specified, the use of an open connection.

Examples

The following example demonstrates the use of PHP mysql_query function to delete the database:

<html>
<head>
<meta charset="utf-8"> 
<title>删除 MySQL 数据库</title>
</head>
<body>
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('连接失败: ' . mysql_error());
}
echo '连接成功<br />';
$sql = 'DROP DATABASE w3big';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('删除数据库失败: ' . mysql_error());
}
echo "数据库 w3big 删除成功\n";
mysql_close($conn);
?>
</body>
</html>

Note: When you delete a database using PHP script, does not appear to confirm whether to delete a message, just delete the specified database, when you delete a database to be especially careful.