Access mysql insert id during insert

Is there a way to access the auto-increment id for use inside a query.

for example, Wordpress puts the insert identifier in the GUID of the same row, is there a way that I can access the identifier that the row will accept during insertion.

Or maybe there is some quick way to find out what the current AI counter is, so I know what will happen in this case?

I need this because I'm trying to fill the WP posts table into a new custom post type that I'm using, and I need the insert identifier to fit inside the GUID column.

+7
source share
7 answers

In the end, I just pulled out the last id like this:

$this->lID = mysql_fetch_assoc(mysql_query("SELECT ID FROM wp_posts ORDER BY ID DESC LIMIT 1")); $this->lID = $this->lID[ID]; 
-3
source
+1
source

I assume you need some sort of column with the same value as the user id. But why do you have to do this? just use the user id, even if you use it, for example, as the link id.

0
source

AFAIK, you cannot access the auto_increment value of the inserted row. I suggest inserting a line, then reading its identifier and updating it to fill in the fields you need to know for the identifier.

(Why do you need this? It seems your database needs some normalization if you have such a problem with chicken eggs.)

0
source

This is one way to do this. This worked for me:

1) just do two separate MySql queries. The first inserts the post values ​​into the wp_posts table. You must insert in all columns except the "guid" column, insert some kind of placeholder, for example, "0".

2) the second request is executed immediately after the first, and here you can call the LAST_INSERT_ID () function to get the true last insert identifier of the previous request. This second request updates the "guid" field with the correct identification number.

You can insert this into a loop to bulk insert WP entries from a csv file, for example.

You must do this when the site is down or blocked at first, so no other request request is between the form somewhere else.

Example: first query (sorry to format the column, but it's easier to debug):

 mysql_query("INSERT INTO wp_posts( ID, post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_content_filtered, post_parent, guid, menu_order, post_type, post_mime_type, comment_count ) VALUES ( 'null', '1', '$timestmp', '$timestmp', '$content', '$posttitle', '', 'publish', 'open', 'open', '', '$postname', '', '', '$timestmp', '$timestmp', '', '0', '$uiai', '0', 'post', '', '0' )") or die(mysql_error()); 

Second request:

 $uid = mysql_insert_id(); $uiai = 'http://yoursite.net/wordpress/?post_type=post&p='.$uid; mysql_query("UPDATE wp_posts SET guid = '$uiai' WHERE ID = '$uid'"); 

... also, when you iterate over a lot of inserts, sometimes it can maximize the maximum allowable runtime for a PHP script, and loading is interrupted. This helps to use set_time_limit (0); throughout the script, so the maximum allowed time counter will be reset.

... hope this helps :)

0
source

Perhaps you could use the AUTO_INCREMENT value of your table in information_schema.tables in an INSERT ... SELECT query. Theoretically, INSERT ... SELECT should block information_schema.tables , and the value of AUTO_INCREMENT should be the identifier that is used for the next INSERT . But you are unlikely to experience this and consider performance issues.

0
source
 use mysql_insert_id() to get last inserted id 

You can see more information at: http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

-one
source

All Articles