How can we delete a MySQL database by using PHP script?


As we know that PHP provides us the function named mysql_query to delete an existing database.

Example

To illustrate this we are deleting a database named ‘Tutorials’ with the help of PHP script in the following example −

<html>
   <head>
      <title>Deleting MySQL Database</title>
   </head>
   <body>
      <?php
         $dbhost = 'localhost:3036';
         $dbuser = 'root';
         $dbpass = 'rootpassword';
         $conn = mysql_connect($dbhost, $dbuser, $dbpass);
         
         if(! $conn ) {
            die('Could not connect: ' . mysql_error());
         }
         echo 'Connected successfully<br />';
         $sql = 'DROP DATABASE TUTORIALS';
         $retval = mysql_query( $sql, $conn );
         
         if(! $retval ) {
            die('Could not delete database: ' . mysql_error());
         }
         echo "Database TUTORIALS deleted successfully
";          mysql_close($conn);       ?>    </body> </html>

Updated on: 22-Jun-2020

129 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements