最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - $wpdb->insert_id

programmeradmin6浏览0评论

I'm using $wpdb->insert to insert data to my plugin table. How can I get the id of the new line I Just insert. Because I have a lot of users that add data to the table I can't use $wpdb->insert_id Because it's not necessary the last one.

I'm using $wpdb->insert to insert data to my plugin table. How can I get the id of the new line I Just insert. Because I have a lot of users that add data to the table I can't use $wpdb->insert_id Because it's not necessary the last one.

Share Improve this question asked Nov 2, 2016 at 9:32 user186585user186585 2011 gold badge2 silver badges2 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

There is no other way, but I can not see how you need any other way then this. I believe that the insert ID accessed by $wpdb is the last ID by this instance of WPDB, other inserts should not affect that but I'm not sure.

<?php

  $wpdb->insert("QUERY");

  $this_insert = $wpdb->insert_id;

?>

$wpdb->insert_id is global withint the database session. since Wordpress creates a new session per user session this is not a problem, even when using a connection pooling. the only caveat is that this variable will always have the last inserted auto-increment ID, so you have to check after each insert. If you really want to capture the list of new inserted records, you can do it inside a stored procedure and capture it in a transaction. or inside a trigger.

发布评论

评论列表(0)

  1. 暂无评论