“PDO::lastInsertId”的替代方案 /“mysql_insert_id”

发布于 2024-07-09 02:40:57 字数 426 浏览 16 评论 0原文

我总是听说使用“lastInsertId”(或者 mysql_insert_id() 如果你不使用 PDO)是邪恶的。 如果是触发器,显然是这样,因为它可能返回完全不是您的 INSERT 创建的最后一个 ID 的内容。

$DB->exec("INSERT INTO example (column1) VALUES ('test')");
// Usually returns your newly created ID.
// However when a TRIGGER inserts into another table with auto-increment:
// -> Returns newly created ID of trigger's INSERT
$id = $DB->lastInsertId();

还有什么选择呢?

I always hear that using "lastInsertId" (or mysql_insert_id() if you're not using PDO) is evil. In case of triggers it obviously is, because it could return something that's totally not the last ID that your INSERT created.

$DB->exec("INSERT INTO example (column1) VALUES ('test')");
// Usually returns your newly created ID.
// However when a TRIGGER inserts into another table with auto-increment:
// -> Returns newly created ID of trigger's INSERT
$id = $DB->lastInsertId();

What's the alternative?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一身软味 2024-07-16 02:40:58

恕我直言,它只是被认为是“邪恶的”,因为几乎没有任何其他 SQL 数据库(如果有的话)拥有它。

就我个人而言,我发现它非常有用,并且希望我不必在其他系统上诉诸其他更复杂的方法。

IMHO it's only considered "evil" because hardly any other SQL database (if any) has it.

Personally I find it incredibly useful, and wish that I didn't have to resort to other more complicated methods on other systems.

风铃鹿 2024-07-16 02:40:58

一种替代方法是使用序列,因此您可以在执行插入之前自行生成 ID。

不幸的是,MySQL 不支持它们,但像 Adodb 这样的库可以使用另一个表来模拟它们。 然而,我认为模拟本身将使用lastInsertId()或等效的...但至少你不太可能在纯粹用于序列的表上有一个触发器

One alternative is to use sequences instead, so you generate the ID yourself before you do the insert.

Unfortunately they are not supported in MySQL but libraries like Adodb can emulate them using another table. I think however, that the emulation itself will use lastInsertId() or equivalent... but at least you are less likely to have a trigger on a table which is purely used for a sequence

走走停停 2024-07-16 02:40:58

如果您采用 ADOdb (http://adodb.sourceforge.net/) 的路线,那么您可以事先创建插入 ID,并在插入时明确指定该 ID。 这可以移植地实现(ADOdb 支持大量不同的数据库...)并保证您使用正确的插入 ID。

PostgreSQL SERIAL 数据类型类似,只是它是按表/按序列的,您可以在请求时指定想要最后插入 ID 的表/序列。

If you go the route of ADOdb (http://adodb.sourceforge.net/), then you can create the insert ID before hand and explicitly specific the ID when inserting. This can be implemented portably (ADOdb supports a ton of different databases...) and guarantees you're using the correct insert ID.

The PostgreSQL SERIAL data type is similar except that it's per-table/per-sequence, you specify the table/sequence you want the last insert ID for when you request it.

谜泪 2024-07-16 02:40:58

我想这也不是最先进的,但我使用写锁来确保我确实获得了最后插入的 ID。

I guess it's not really state of the art either but I use write locks to make sure that I really get the last inserted ID.

瞎闹 2024-07-16 02:40:58

它并不复杂,效率也不高,但如果您插入的数据包含唯一字段,那么 SELECT 显然可以产生您想要的结果。

例如:

INSERT INTO example (column1) VALUES ('test');
SELECT id FROM example WHERE column1 = 'test';

It's not sophisticated and it's not efficient, but if the data you've inserted include unique fields, then a SELECT can obviously yield what you're after.

For example:

INSERT INTO example (column1) VALUES ('test');
SELECT id FROM example WHERE column1 = 'test';
瘫痪情歌 2024-07-16 02:40:58

你可以试试这个:

$sql = "SELECT id FROM files ORDER BY id DESC LIMIT 1";
$PS = $DB -> prepare($sql);
$PS -> execute();
$result = $PS -> fetch();

You could try this:

$sql = "SELECT id FROM files ORDER BY id DESC LIMIT 1";
$PS = $DB -> prepare($sql);
$PS -> execute();
$result = $PS -> fetch();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文