PHP & MySQL Tutorial PHP & MySQL References

PHP & MySQL - Delete Table



A database consists of one or more tables. The DROP TABLE statement is used to delete a table from the MySQL database. For example, to delete a table named "Employee", the following query can be used:

DROP TABLE Employee

To connect to the MySQL database, mysqli_connect() function can be used. After establishing the connection to the database, mysqli_query() function can be used to perform the query on the database to delete the given table.

Delete a MySQL Table - Object-oriented style

The example below demonstrates how to delete a table named "Employee" in object-oriented style.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDatabase";

//establishing connection 
$mysqli = new mysqli($servername, $username, $password, $dbname);

//checking connection
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

//query for deleting table
$sql = "DROP TABLE Employee";

//executing the query
if (!$mysqli->query($sql)) {
  echo "Error deleting table: ". $mysqli->error;
} else { 
  echo "Table Employee deleted successfully.";
}

//closing the connection
$mysqli->close();
?>

Delete a MySQL Table - Procedural style

To obtain the same result using procedural style, the following script can be used.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDatabase";

//establishing connection 
$mysqli = mysqli_connect($servername, $username, $password, $dbname);

//checking connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

//query for deleting table
$sql = "DROP TABLE Employee";

//executing the query
if (!mysqli_query($mysqli, $sql)) {
  echo "Error deleting table: ". mysqli_error($mysqli);
} else { 
  echo "Table Employee deleted successfully.";
}

//closing the connection
mysqli_close($mysqli);
?>

Complete PHP MySQLi Reference

For a complete reference of all properties, methods and functions of PHP MySQLi extension, see PHP MySQLi Reference.