Questions tagged [lastinsertid]
135 questions
5
votes
1 answer
PHP/MYSQL how to get last_insert_id in transaction
I have 2 MySQL tables
table#1 - container:
container_id INT (PK AI)
start_time DATETIME
table#2 - reservation
reservation_id INT (PK AI)
reservation_time DATETIME
container_id INT (FK)
The PHP code:
mysqli_query($dbc,'SET…
![](../../users/profiles/1523421.webp)
bilasek
- 77
- 2
- 4
4
votes
3 answers
When will we give an argument to LAST_INSERT_ID()?
Quoted from here:
mysql> UPDATE sequence SET id=LAST_INSERT_ID(id+1);
mysql> SELECT LAST_INSERT_ID();
What's the use case of the above (what's the benefit )?
I've never used the LAST_INSERT_ID with any parameter before...
UPDATE
My question is why…
![](../../users/profiles/851277.webp)
asker
- 2,101
- 3
- 19
- 26
4
votes
2 answers
Is bulk insert atomic?
I have table with auto increment primary key. I want insert a bunch of data into it and get keys for each of them without additional queries.
START TRANSACTION;
INSERT INTO table (value) VALUES (x),(y),(z);
SELECT LAST_INSERT_ID() AS…
![](../../users/profiles/421010.webp)
Andrew
- 7,680
- 11
- 42
- 73
4
votes
2 answers
mySQL 5.0.45 LAST_INSERT_ID() and values larger than a signed int
I'm attempting to use LAST_INSERT_ID on an auto incremented index that has moved past the signed int value 2147483647. This column is an unsigned int. However, LAST_INSERT_ID() is returning an invalid negative value. Researching this, I've found a…
![](../../users/profiles/310364.webp)
EricLeslie
- 67
- 4
4
votes
3 answers
Last inserted _key in ArangoDB with AQL?
How can i receive a last inserted _key in ArangoDB with AQL query?
I put the item in the collection, the following element must contain _key created element. How do I get this _key?
![](../../users/profiles/3340407.webp)
jonua
- 1,569
- 1
- 13
- 25
3
votes
2 answers
Race Conditions with mysql_last_id()
I've spent the better part of the day trying to find an answer to this, but there just doesn't seem to be one out there. I have need of function which creates a row in a table, retrieves the auto-increment from the inserted row, and then uses that…
![](../../users/profiles/858061.webp)
Michael Fenwick
- 1,924
- 1
- 16
- 24
3
votes
2 answers
How can I use PostgreSql's CURRVAL / RETURNING from another php file?
I'm running an INSERT query from one php file, but then I need to get the last inserted id from an external php file so as to run another query based on that id. How can I pull this off using CURRVAL or RETURNING? From the external php file, if I do…
![](../../users/profiles/5972831.webp)
Clint_A
- 428
- 1
- 8
- 21
3
votes
6 answers
Last inserted record ID problems using typed DataSet and DataGridView
I'm using following for a simple database application:
SQL Server Compact Edition .sdf file as database, with int primary key IDs.
Typed DataSet and BindingSource as data access layer
DataGridView for displaying data.
My problem is, I'm having…
Armagan
3
votes
3 answers
how do i handle concurrent inserts in mysql table and fetch the correct insert id
I am using adodb for PHP library.
For fetching the id at which the record is inserted I use this function "$db->Insert_ID()"
I want to know if there are multiple and simultaneous inserts into the database table, will this method return me the…
![](../../users/profiles/104698.webp)
Gaurav Sharma
- 2,828
- 1
- 35
- 54
3
votes
3 answers
Transaction with two sql insert
I have two sql insert to do (say for examples in tables A and B), they are in a transaction because I want the database to remain consistent, that is, a tuple in A must have references in B.
In the second insert I need the id that comes from the…
![](../../users/profiles/57095.webp)
Alberto Zaccagni
- 28,473
- 10
- 71
- 102
3
votes
2 answers
Zend / ZF2 / TableGateway mysql_insert_id replacement?
I am trying to switch from mysql to Zend / TableGateway class.
I wonder if there is a method, similar to mysql_insert_id, to get the autoincremented id of the last inserted row.
Googling around, I found some answers, pointing to the lastInsertId()…
![](../../users/profiles/1092981.webp)
Gisela
- 1,144
- 2
- 13
- 29
3
votes
3 answers
PDO + PHP lastInsertId() issue
Wouldn't there be a problem with it if for example when a user clicks on a link, a new row is automatically inserted and then the php code requests the last inserted id, and at the same time another row is inserted by another user, so the returned…
![](../../users/profiles/685767.webp)
Asaf
- 1,887
- 7
- 32
- 56
2
votes
2 answers
Kohana 3 ORM retrieve last insert ID
Is there a way after the ->save() call to get the last insert id?
Example Code:
$post_data = $_POST;
$user = ORM::factory('user');
$user->username = $post_data['username'];
$user->email = $post_data['email'];
$user->save();
![](../../users/profiles/920331.webp)
12Bo
- 106
- 1
- 3
- 11
2
votes
3 answers
SQLite get last row ID after connection is closed
I have two functions.
One Insert into database. It opens the connection and closes it after insert.
Other is GetLastID function.
I know I could query the biggest ID but I was told it's a bad practice.
"SELECT sqlite_last_insert_rowid();" seems no…
![](../../users/profiles/664066.webp)
Gubi
- 393
- 2
- 7
- 19
2
votes
1 answer
PDO Mysql prepared statement last_insert_id returns wrong value on multi-insert?
I noticed that if I prepare a multi-insert statement and execute it into MySQL via PDO, and then request the last_insert_id, I get the first ID of the multiple inserted rows, not the last one. Specifically:
"INSERT INTO test_table (value1, value2,…
![](../../users/profiles/504357.webp)
Swader
- 10,807
- 14
- 46
- 82