Home » Php » php – accessing ID of mysql insert during the insert

php – accessing ID of mysql insert during the insert

Posted by: admin July 12, 2020 Leave a comment

Questions:

Is there any way to access the id of the auto increment for use inside the query.

for instance WordPress puts the id of the insert in the GUID of the same row, is there any way i can access the id that the row will take during the insertion.

Or maybe there is some quick way for me to find out what the current AI counter is at, so I know what the one going in will be?

I need this because I am trying to backfill the WP posts table into a new custom post type I am using, and I need to have the id of the insert to put inside the GUID column.

How to&Answers:

Maybe the mysql LAST_INSERT_ID() function can help you.
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id

Answer:

I assume that you want some sort of column with the same value as the user ID.
But why should you do that? just use the user ID, even if you use it for example as the refer ID.

Answer:

AFAIK, you can’t access the auto_increment value of a row being inserted. I suggest to insert the row, then read its ID and update it to fill the fields that you need to know the ID for.

(Why do you need that? It seems your database needs some normalization, if you have such an egg-chicken problem.)

Answer:

This is one way of doing it. This worked for me:

1) just execute 2 separate MySql queries. The first one inserts the post values into the wp_posts table. You should insert into all columns except in the column “guid” insert some placeholder, like “0”.

2) the second query is executed right after the first one and here you can call the LAST_INSERT_ID() function to get the true last insert ID of the previous query. This second query updates the “guid” field with the right ID number.

You can insert this into the loop for mass-inserting of WP posts from csv file for example.

You should do this when the site is off-line or lock the tables first so no other query request comes in between form somewhere else.

Example: 1st query (sorry for the column formatting but it is 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()); 

2nd query:

$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 are looping through many inserts, sometimes it can max out the maximum allowed execution time for PHP script and the upload gets interrupted. It helps to use set_time_limit(0); throughout the script so the max allowed time counter is regularly reset.

…hope this helps 🙂

Answer:

Perhaps you could use AUTO_INCREMENT value of your table in information_schema.tables in an INSERT ... SELECT query. In theory INSERT ... SELECT should lock information_schema.tables and AUTO_INCREMENT value should be the Id which is used for next INSERT. But you should hardly test this and consider performance issues.

Answer:

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

Answer:

I ended up just pulling out the last id like so:

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