简体   繁体   中英

How do I delete the last record inserted into a MySQL table using PDO?

How do I use PDO to delete the last row inserted (this was done in another class)?

I have this, but I don't know what else I'm supposed to do with it:

$pdo = new PDO("mysql:host=$db_host;dbname=$db_name;", $db_user, $db_password);
$stmt = $pdo->prepare('DELETE FROM Resources');
$stmt->execute();

After inserting a new record, you get the ID like this:

$insert = $pdo->prepare("INSERT INTO Resources (data) VALUES (:data)");
$insert->execute(array(':data' => $data));
$lastid = $pdo->lastInsertID();

// delete last entry
$delete = $pdo->prepare("DELETE FROM Resources WHERE id = :id LIMIT 1");
$delete->execute(array(':id' => $lastid));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM