简体   繁体   English

如何使用PDO删除插入MySQL表的最后一条记录?

[英]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)? 如何使用PDO删除插入的最后一行(这是在另一个类中完成的)?

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: 插入新记录后,您将获得如下ID:

$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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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