Deleting Records with PDO
1. Deleting Records with PDO
Here in this step, you will learn about delete query. Delete query is also same as select SQL query. Instead of the select query, you can use the delete SQL query.
With exec method, you will get no of affected rows.
$sql = "DELETE FROM crud WHERE lastname='string-here'";
$delete = $db->exec($sql);
echo $delete . " Records Deleted from DB ";
After executing the above code, you will get the number of deleted rows.
Other Articles in Learn PHP PDO
- PDO Basics
- Fetching Records with PDO
- Inserting Records with PDO
- Deleting Records with PDO
- PDO Prepared Statements
- CRUD Application in PHP PDO
2. When to use Query & Execute Method
The difference between query and execute method is query method can be used for select queries. And for non-select queries that is insert, update and delete to get the number of affected rows you can use execute method.