我需要检索上次查询的 AUTO_INCREMENT 值的 ID。
我的查询是这样的:
$result = $wpdb->query( $wpdb->prepare( "
INSERT INTO $table_name
( name, season, copyright, description, path )
VALUES ( %s, %s, %s, %s, %s )",
$galleryData['name'], $galleryData['season'], $galleryData['copyright'], $galleryData['description'], $galleryData['path'] ) );
// Let's output the last autogenerated ID.
echo $wpdb->insert_id;
// This returns the same result
echo mysql_insert_id();
查看我的数据库表,我看到行数从 1 到 24(24 行)。
但是使用 $wpdb->insert_id
或 mysql_insert_id()
返回 241
。
进行新的插入将返回 251、261、271 等。为什么我要在末尾添加额外的“1”?
更新
多亏了 Pekka(我最好在我欠他的啤酒数量上运行一个标签),我想通了。
进一步深入我得到的代码:
if(!$result)
_e("[DB error] Ups. Something went wrong when trying to insert the event.");
else
echo true;
输出的是最后一条语句(echo true)!
最佳答案
我打赌美味的科隆啤酒:
原因实际上是代码后面的 echo
,它回显了一些 bool 变量,其 true
将转换为 1
。
如果我错了,我会像个男人一样接受反对票。
(原谅我愚蠢的回答,已经很晚了,我还在工作:)
更新:啊,快乐,我是对的!
关于php - 为什么 $wpdb->insert_id 和 mysql_insert_id() 最后返回一个额外的 '1'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3964318/