DROP Query using PDO

PHP PDO & MYsQL We can drop or delete table from Database by using DROP query.


  • PDO: rowCount() with INSERT, DROP & DELETE query


Here is an example.
Database connection is available at config.php file.
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.

Example 1: Error Handling in DROP

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();
}

Example 2: Check if Table Exists Before Dropping

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.";
}

  • DELETE:
    • Removes rows one by one.
    • Can include a WHERE clause to delete specific records.
    • Slower since it logs each row deletion.
    • Can trigger AFTER DELETE triggers.
    • rowCount() works and returns the number of rows deleted.
  • TRUNCATE:
    • Removes all rows from the table.
    • Cannot include a WHERE clause.
    • Faster, as it doesn’t log individual deletions.
    • Resets the auto-increment counter.
    • Does not trigger DELETE triggers.
    • rowCount() does not work, as it doesn't return row count.
  • DROP:
    • Completely removes the entire table from the database.
    • All data, structure, and indexes are removed.
    • Cannot be rolled back if not in a transaction.
    • Cannot use rowCount() as the table itself is deleted.

Transaction

We cannot use transactions with DROP TABLE in most databases, including MySQL, because DROP TABLE implicitly commits the transaction. Once a table is dropped, the action cannot be rolled back, even if it's inside a transaction block.
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();
}

PDO References Delete Records


Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate 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.



Subscribe to our YouTube Channel here



plus2net.com







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.




PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer