it is very easy to drop an existing mysql table, but you need to be very careful while deleting any existing table because the data lost will not be recovered after deleting a table.
syntax
here is a generic sql syntax to drop a mysql table −
drop table table_name ;
dropping tables from the command prompt
to drop tables from the command prompt, we need to execute the drop table sql command at the mysql> prompt.
example
the following program is an example which deletes the tutorials_tbl −
root@host# mysql -u root -p enter password:******* mysql> use tutorials; database changed mysql> drop table tutorials_tbl query ok, 0 rows affected (0.8 sec) mysql>
dropping tables using php script
php uses mysqli query() or mysql_query() function to drop a mysql table. this function takes two parameters and returns true on success or false on failure.
syntax
$mysqli→query($sql,$resultmode)
| sr.no. | parameter & description |
|---|---|
| 1 |
$sql required - sql query to drop a table. |
| 2 |
$resultmode optional - either the constant mysqli_use_result or mysqli_store_result depending on the desired behavior. by default, mysqli_store_result is used. |
example
try the following example to drop a table −
copy and paste the following example as mysql_example.php −
<html>
<head>
<title>dropping mysql table</title>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root@123';
$dbname = 'tutorials';
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if($mysqli→connect_errno ) {
printf("connect failed: %s<br />", $mysqli→connect_error);
exit();
}
printf('connected successfully.<br />');
if ($mysqli→query("drop table tutorials_tbl")) {
printf("table tutorials_tbl dropped successfully.<br />");
}
if ($mysqli→errno) {
printf("could not drop table: %s<br />", $mysqli→error);
}
$mysqli→close();
?>
</body>
</html>
output
access the mysql_example.php deployed on apache web server and verify the output.
connected successfully. table tutorials_tbl dropped successfully.