为什么$ wpdb-> insert_id和mysql_insert_id()最后返回一个额外的'1'?

时间:2010-10-18 23:37:42

标签: php wordpress

我需要检索上一个查询的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_idmysql_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)!

1 个答案:

答案 0 :(得分:14)

我打赌美味的Kölsch:

alt text

原因实际上是代码后面的echo,它回应了一些布尔变量,其true将转换为1

如果我错了,我会像男人一样接受投票。

(原谅这个愚蠢的回答,已经很晚了,我还在工作:)

更新:啊,快乐,我是对的!