Search results
1 paź 2010 · You could store the last insert id in a variable : INSERT INTO table1 (title,userid) VALUES ('test', 1); SET @last_id_in_table1 = LAST_INSERT_ID(); INSERT INTO table2 (parentid,otherid,userid) VALUES (@last_id_in_table1, 4, 1); Or get the max id from table1 (EDIT: Warning.
If we perform an INSERT or UPDATE on a table with an AUTO_INCREMENT field, we can get the ID of the last inserted/updated record immediately. In the table "MyGuests", the "id" column is an AUTO_INCREMENT field:
Throughout this article, we will be exploring a PHP solution for retrieving a MySQL database’s last inserted ID by using the PDO extension in order to retrieve the ID from that database. We are also going to highlight some best practices that can be applied when working with ID generation and retrieval in MySQL databases so that we avoid ...
In this tutorial you will learn how to retrieve the unique ID of the last inserted row from a MySQL database table using PHP. In the PHP MySQL insert chapter you've learnt MySQL automatically generate an unique ID for the AUTO_INCREMENT column each time you insert a new record or row into the table.
In this article, we have shown you how to retrieve the last inserted ID in PHP and MySQL. By following these steps, you should be able to retrieve the ID of the last inserted record in your database.
The LAST_INSERT_ID() function returns the AUTO_INCREMENT id of the last row that has been inserted in a table. Syntax
13 gru 2012 · However, we can also use LAST_INSERT_ID() directly in other MySQL instructions, for example to insert the last ID into a table: INSERT INTO tab (id, pid) VALUES (1, LAST_INSERT_ID()); Or to be able to access the data record just inserted in another instruction: INSERT INTO tab (fname) VALUES ('Anne'); UPDATE tab SET fname = 'Anna' WHERE id ...