Delete MySQL records using PDO in PHP

July 07, 2013 | 860 views | Comments

It's easy to delete a MySQL record using PDO in PHP, just make sure a PDO connection to the MySQL database has been successfully established, and more importantly, use the prepare() function to auto escape any parameter values. Select from the named (:name) or the question mark (?) parameter marker to bind any user-input value to the query.

For example.

Delete a record using the named (:name) parameter where id is a user-input value.

$id = $_GET['id'];
$sth = $dbh->prepare("DELETE FROM TABLE WHERE id = :id");
$sth->execute(array(":id"=>$id));

Delete a record using the question mark (?) parameter where id is a user-input value.

$id = $_GET['id'];
$sth = $dbh->prepare("DELETE FROM TABLE WHERE id = ?");
$sth->execute(array($id));