require "config.php"; // database connection
$sql=$dbo->prepare("DROP TABLE student_del ");
$sql->execute();
echo "Table dropped successfully.";
After the query execution we will add message saying success or print error message in case of failure.
<?Php
require "config.php"; // database connection
$sql=$dbo->prepare("DROP TABLE student ");
if($sql->execute()){
echo " Table deleted ";
}else{
print_r($sql->errorInfo());
}
?>
In the above code we have used database connection code inside config.php file.
Using a try-catch block to handle errors when dropping a table.
try {
$sql = "DROP TABLE IF EXISTS students";
$pdo->exec($sql);
echo "Table dropped successfully.";
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
}
This example checks if a table exists before trying to drop it.
$result = $pdo->query("SHOW TABLES LIKE 'students'");
if($result->rowCount() > 0) {
$pdo->exec("DROP TABLE students");
echo "Table dropped successfully.";
} else {
echo "Table does not exist.";
}
try {
$pdo->beginTransaction();
$pdo->exec("DROP TABLE IF EXISTS students");
// Any further database operations here
$pdo->commit();
} catch (PDOException $e) {
$pdo->rollBack(); // Cannot roll back DROP TABLE
echo "Error: " . $e->getMessage();
}
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.
aliko | 25-01-2016 |
What about if I want to TRUNCATE A TABLE how would I do that? cheers |
smo1234 | 25-01-2016 |
Details about the TRUNCATE command is here. |