Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does PDO know last inserted id in MySQL?

EDIT: This question title originally was: How does Doctrine know last inserted id in MySQL? and was related to Doctrine ORM mapper. After some digging I found out that this question is not related to Doctrine but to PDO_MySQL, MySQL C API and finally - to MySQL client-server communication. I have decided to change the title, so maybe someone will find answer to his/hers question.

For those who are not using Doctrine: I was curious, why bellow:

mysql_query("INSERT INTO category (name) VALUES('cat')");
echo mysql_insert_id();

or similar:

$pdo->exec("INSERT INTO category (name) VALUES('cat')");
echo $pdo->lastInsertId();

will lead to only one position (without separate SELECT LAST_INSERT_ID()) in log:

1701 Query    INSERT INTO category (name) VALUES ('cat')

Original question:

I have 2 tables:

category(id,name)
product(id, name, categoryId)

I created new category object and product object. I assigned category object to product object. I didn't set any ids:

$product = new Product();
$product->name = 'asdf';

$category = new Category();
$category->name = 'cat';

$product->Category = $category;

After that I flushed the connection and check MySQL logs:

1684 Query  START TRANSACTION
1684 Query  INSERT INTO category (name) VALUES ('cat')
1684 Query  INSERT INTO product (name, categoryid) VALUES ('asdf', '312')
1684 Query  COMMIT

How did Doctrine know, that the newly created category id is 312? There is nothing else in logs.

like image 560
prostynick Avatar asked Nov 24 '10 13:11

prostynick


People also ask

How can I see last inserted record in MySQL?

If you are AUTO_INCREMENT with column, then you can use last_insert_id() method. This method gets the ID of the last inserted record in MySQL.

How do I get the latest inserted record ID in SQL?

SELECT IDENT_CURRENT('tablename') It returns the last IDENTITY value produced in a table, regardless of the connection that created the value, and regardless of the scope of the statement that produced the value. IDENT_CURRENT is not limited by scope and session; it is limited to a specified table.

Which function returns last inserted ID in MySQL?

The LAST_INSERT_ID() function returns the AUTO_INCREMENT id of the last row that has been inserted or updated in a table.

How do you find the last inserted record from a table?

How do I get last inserted data? you can get the id if you call LAST_INSERT_ID() function immediately after insertion and then you can use it. Show activity on this post. For any last inserted record will be get through mysql_insert_id() If your table contain any AUTO_INCREMENT column it will return that Value.


2 Answers

I did some research and browse some source code, so my answer could be a little bit wrong and not precise.

First of all, this is not really related to Doctrine. Doctrine uses PDO_MYSQL. But internally PDO_MYSQL uses the same thing as mysql_insert_id function - native MySQL C API function - mysql_insert_id.

The reason why there is no seperate SELECT LAST_INSERT_ID() lies in the fact, that after the statement is executed (in my example INSERT), server responds with data and some other things included in OK Packet), including insert_id. So when we fire mysql_insert_id() we are not connecting to the server, to receive insert_id - mysql library does not need to do that - it already has this value stored from last execution of query (at least I think so after analyzing the file libmysql.c)

OK Packet is described here: MySQL Generic Response Packets - OK Packet

like image 129
prostynick Avatar answered Oct 08 '22 14:10

prostynick


Probably by calling http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

like image 20
Tobias Avatar answered Oct 08 '22 15:10

Tobias