MySQL / PHP: Get value of AUTO_INCREMENT for new record

Here is the structure of the MySQL database I am using:
CREATE TABLE `xyz` (
  `xyz_id` int(11) NOT NULL AUTO_INCREMENT,
  `x` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `y` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `z` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`xyz_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Open in new window


Using PHP, how can I determine the value of `xyz_id` for the record that was inserted, without risking a race-condition problem?
  $sql = "INSERT INTO `xyz` (
     `x`, 
     `y`,
     `z`
   ) VALUES (
     'x',
     'y',
     'z'
   )";
  $result = $crm_db->query($sql);
  echo 'The `xyz_id` of the most recently added record is: ???';

Open in new window

LVL 10
skijAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
What lib are you using

mysql - (mysql_insert_id) NB this library has been deprecated and you should update it to mysqli / pdo
http://php.net/manual/en/function.mysql-insert-id.php

PDO::lastInsertId
http://php.net/manual/en/pdo.lastinsertid.php

MySQLI (insert_id)
http://php.net/manual/en/mysqli.insert-id.php

There won't be a race condition because the last insert id is set by session
http://php.net/manual/en/mysqli.insert-id.php
0
skijAuthor Commented:
I am using MySQLI (insert_id).  

Are you sure this will not risk a race condition?
echo 'The `xyz_id` of the most recently added record is: ' .  $crm_db->insert_id;
  

Open in new window

0
Julian HansenCommented:
Are you sure this will not risk a race condition?
Absolutely, as I said that value set per session - so if another user is accessing the page - they have a different session and will get the last_insert_id that was generated in their session.

User A connects and inserts record: Last value was 1 - so new record has value 2.
insert_id for User A's session is set to 2

At the same time User B connects and inserts a record: Database handles the auto increment so record gets value of 3. insert_id is now set to 3 in User B's session

A asks for insert_id - it is always going to be 2 because his session is completely separate from User B.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
This and many other things about using MySQL (not the extension, but the database, which we use with MySQLi or PDO) are spelled out in detail in this article, with code examples for all of them.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

See the code examples under "Use an Iterator to Escape the Data and Load the Table"
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.