gpt4 book ai didi

php - 在 wordpress wpdb 中返回插入的行 ID

转载 作者:行者123 更新时间:2023-11-29 05:09:18 28 4
gpt4 key购买 nike

我想将一些数据插入到 wordpress 的表中,然后返回它的自动增加 id 值。

我这样做了,但是这两个都有错误:

INSERT INTO wp_sho_app_form (job_id, first_name, last_name) VALUES(1, 'Caldwell', 'Estrada'); 
SELECT LAST_INSERT_ID();

这个代码字在 MySql 中很好但是通过 $wpdb 我得到这个错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT LAST_INSERT_ID()' at line 1

我用$wpdb->query$wpdb->get_results 调用这个语句,但是它们都有那个错误。由于某些原因,我不想使用 $wpdb->insert,我应该使用这种类型的代码。

最佳答案

正确的方法是使用insert_id

$lastid = $wpdb->insert_id;

如果您有新条目和大量流量,如果您进行另一个查询,则它不一致。这可能会导致有线问题。

这仅在插入后有效,您应该使用它们。不要与框架作斗争。

关于php - 在 wordpress wpdb 中返回插入的行 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42244465/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com