Team LiB
Previous Section Next Section

Deleting a Database

The goal of this script is to delete a database on the MySQL server. To do so, you simply have to modify the query used in the previous script.

  1. Open db_createdb.php in your text editor.

  2. Change the value of the $sql variable to

    $sql = "DROP DATABASE testDB2";
    
  3. Change the error message and the HTML title to reflect the fact that you want to delete a database and not create one.

Your code should look something like this:

<?
$sql = "DROP DATABASE testDB2";

$connection = @mysql_connect("localhost","spike","9sj7En4")
or die(mysql_error());
$result = @mysql_query($sql,$connection) or die(mysql_error());
if ($result) {
     $msg ="<P>Database has been deleted!</P>";
}
?>
<HTML>
<HEAD>
<TITLE>Delete a MySQL Database</TITLE>
</HEAD>
<BODY>
<? echo "$msg"; ?>
</BODY>
</HTML>
  1. Save the file with the new name db_dropdb.php, and place this file in the document root of your web server.

  2. Open your web browser and type http://127.0.0.1/db_dropdb.php.

If the database deletion was successful, you'll see the message shown in the following figure.

Click To expand

In the next chapter, you create a database table for keeps, and you eventually populate that table with some data.


Team LiB
Previous Section Next Section