Delete Query using PDOWE can delete a record using delete query through PDO connection We assumed that config.php file is already available and it is connected to database . mem_id is the unique id of the record which is to be deleted.
In the above code one record will be deleted from the table based on the id number stored in the variable $mem_id. We can also delete more records matching to a condition. Let us try to delete all records having id number more than 10.
Here is the code.
All records having id more than 10 will be deleted. We can also know how many records are deleted by using rowCount() command. This command will return a numeric value saying us number of records deleted by the query just given. Here is the sample code.
DELETE with Error MessageTo display the error message we will modifiy the above query like this.
Deleting all the records of a tableWe can empty any table by using TRUNCATE command. But after deleting we will not get number of records deleted by using rowCount(). Read More on TRUNCATE command
Here is the code .
Above code will delete all records but we won't get any output saying number of records deleted.
By using delete command we can get the number of records deleted in the table.
Use this line ( by removing comment from this and adding comment to TRUNCATE query ) to get number of records deleted.