开发者

Why is $wpdb->insert_id and mysql_insert_id() returning with an extra '1' at the end?

开发者 https://www.devze.com 2023-01-20 21:28 出处:网络
I need to retrieve the ID of the AUTO_INCREMENT value of the last query. My query looks like this: $result = $wpdb->query( $wpdb->prepare( \"

I need to retrieve the ID of the AUTO_INCREMENT value of the last query.

My query looks like this:

  $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();
开发者_如何学JAVA

Looking at my DB table, I see rows counting from 1 to 24 (24 rows). But using $wpdb->insert_id or mysql_insert_id() returns 241.

Doing new inserts will return 251, 261, 271 and so on. Why do I et the extra '1' at the end?

UPDATE

Thanks to Pekka (I'd better stad running a tab on the number of beers I owe him), I figured it out.

Further down the code I got this:

  if(!$result)
    _e("[DB error] Ups. Something went wrong when trying to insert the event.");
  else
    echo true;

It's the last statement (echo true) that is outputed!


I am betting a delicious Kölsch:

Why is $wpdb->insert_id and mysql_insert_id() returning with an extra '1' at the end?

that the reason actually is an echo later in the code, that echoes some boolean variable, whose true will translate to 1.

If I am wrong, I shall take the downvotes like a man.

(Forgive the silly answer, it's very late already and I'm still working :)

Update: Ahh, joy, I was right!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号