Questions tagged [last-insert-id]

LAST_INSERT_ID is MySQL specific functionality to get the value of the AUTO_INCREMENT column most recently inserted into. It is the recommended means of getting the value in MySQL, because SELECT MAX(auto_increment) ... is not reliable, as concurrency problems can occur.

LAST_INSERT_ID is MySQL specific functionality to get the value of the AUTO_INCREMENT column most recently inserted into. It is the recommended means of getting the value in MySQL, because SELECT MAX(auto_increment) ... is not reliable, as concurrency problems can occur.

That said, this functionality is not ANSI. Sequences are now ANSI, supported by DB2, Oracle, PostgreSQL, and SQL Server "Denali"). The ANSI equivalent to LAST_INSERT_ID would be: CURRVAL ( NEXTVAL is used to get the next value).

Documentation:

215 questions
3
votes
3 answers

LAST_INSERT_ID not working on UPDATE

if I use this SQL: UPDATE formulare SET EV_id=59, EV_status=5 WHERE EV_id=57 AND ID_uziv=12;SELECT LAST_INSERT_ID(); I will get 0 as last insert id. I'm using php mysqli_insert_id and here is said that: The mysqli_insert_id() function returns the…
koubin
  • 579
  • 5
  • 9
  • 30
3
votes
1 answer

Is AUTO_INCREMENT implemented properly in my case?

This question is bit related to my old question, Getting last record from mysql. Per those answers, I learned that SELECT is not guaranteed to return rows in any specific order (without using an ORDER BY clause, of course). I followed the @YaK…
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
3
votes
1 answer

why pdo->lastInsertId() return 0 when i call STORED PROCEDURE in mysql?

i need return id of last inseted row in my database : i have a class named DatabaseHandler to use pdo
mohsen1932
  • 111
  • 2
  • 11
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()…
Gisela
  • 1,194
  • 2
  • 16
  • 30
2
votes
1 answer

go mysql LAST_INSERT_ID() returns 0

I have this MySQL database where I need to add records with a go program and need to retrieve the id of the last added record, to add the id to another table. When i run insert INSERT INTO table1 values("test",1); SELECT LAST_INSERT_ID() in MySQL…
mcklmo
  • 69
  • 7
2
votes
1 answer

Modified PDO & lastInsertId()

It's just a new form of PDO that I don't fully understand. I know problems similar to this one have been solved on this site. But I’ve been attracted to this new (new to me) class-based system of PDO. It's sleek & concise. I figured everything out,…
2
votes
1 answer

MariaDB Columnstore LAST_INSERT_ID() alternative

Environment: OS: CentOS 7.2 DB server: 10.1.23-MariaDB Columnstore 1.0.9-1 2 test databases, one InnoDB and one Columnstore: CREATE TABLE `test_innodb` ( `ctlid` bigint(20) NOT NULL AUTO_INCREMENT, `rfid` varchar(100) DEFAULT NULL, …
Ciprian Stoica
  • 2,309
  • 5
  • 22
  • 36
2
votes
1 answer

How does Doctrine populate id property of an Entity after successful insert/persist operation?

Using ORM $timer = new Timer(); //an object marked as a Doctrine Entity $this->em->persist($timer); print $timer->getId(); //blank - not yet set $this->em->flush($timer); print $timer->getId(); //prints ID of newly inserted record Actual ORM Code…
Dennis
  • 7,907
  • 11
  • 65
  • 115
2
votes
1 answer

Reliable or not PDO lastInsertId() when using transactions

I use PDO transaction try { DB::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); DB::$db->beginTransaction(); $db->prepare( insert query ); $db->execute(); $last_insert_id = $db->lastInsertId(); ... …
Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236
2
votes
2 answers

Is it possible to get LAST_INSERT_ID() from different database?

Suppose, that we have 2 databases: a and b, and tables a.test1 and b.test2. If I need to insert a row into table a.test1, and return LAST_INSERT_ID() to insert into b.test2, will LAST_INSERT_ID() return value from another database? Is it reliable? I…
BlitZ
  • 12,038
  • 3
  • 49
  • 68
2
votes
1 answer

How to get ID of row which is getting affected when used "REPLACE INTO" in mysql

I am using REPLACE INTO command for inserting/updating record in MySQL table. However I want to get ID of row which gets affected by REPLACE INTO. I used lastInsertId() with INSERT INTO but it is not useful over here because if my record already…
Bharat Jain
  • 29
  • 1
  • 5
2
votes
1 answer

LAST_INSERT_ID() result from stored procedure

I have a stored procedure in mysql which is updating a lot of rows and then inserting one (Hierarchical data structure to be specific). Don't look much at the code except for the last one where I am setting the @insert_id variable. CREATE…
Vega
  • 21
  • 1
  • 4
2
votes
2 answers

getting last inserted row id with PDO (not suitable result)

I have a problem with PDO::lastInsertId() method which doesn't return the id (primary key) of last inserted row, instead it returns another field which is a foreign key field. PHP code: $pdo = new PDO(...); $stmt =…
Mustafa Shujaie
  • 806
  • 2
  • 10
  • 18
2
votes
0 answers

Avoid increasing Auto_increment value?

In MySQL, you can insert a row and update the 'last insert ID' at the same time. I'm using this trick to be able to insert items conditionally (uniqueness) and still get the unique ID in all cases. The code below works. The problem is that the ON…
l33t
  • 18,692
  • 16
  • 103
  • 180
1
vote
1 answer

last_insert_rowid in sqlite - two tables

I got two tables, one of which is a helper of the other. The table "reviews" has a column which is an integer referencing to the id of the film of that review in the table "films". Is there a way to, after inserting in the table "films" a new film,…
user1264201
  • 75
  • 1
  • 9
1 2
3
14 15