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
0
votes
1 answer

MySQL INSERT with ON DUPLICATE SELECT possible?

email_address ------------- id prefix fqdn Prefix and fqdn together form a unique key. I would like to write a query which would do an insert, if the email address is not yet in the table. If the address is already in the table, it should return…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
0
votes
3 answers

Get last insert id from last inserted row

I'm here again and I'm getting crazy! I have this stored procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `aggiungi_categoria`(IN `id_categoria_principale` INT, IN `nome_categoria` VARCHAR(150), IN `sigla_categoria` VARCHAR(10), IN…
Marco C
  • 45
  • 1
  • 8
0
votes
2 answers

issue with insert query in mysql

I have following query : insert into A select last_insert_id(),B.id,C.name,C.address, from sample_table C join other_table B on B.phoneNumber=C.phoneNumber; I am getting duplicate primary key value =1 error ( which should be generated by…
Ashish
  • 14,295
  • 21
  • 82
  • 127
0
votes
1 answer

cakephp getLastInsertID()

This code: foreach ($arrays as $array){ $this->Model->set('bla', $array['key1']); $this->Model->set('alb', $array['key2']); $this->Model->save(); echo $this->Model->getLastInsertID() . ","; } returns: 2,2 should return: 1,2 I…
uldo
  • 19
  • 2
  • 8
0
votes
1 answer

Both sql LAST_INSERT_ID() and PHP insert_id return 0

I've been searching google a lot for this issue and really found nothing. People just keep copying MySQL documentation on last_insert_id and nothing else. I've got an issue regarding last_insert_id, because for both situations (php & sql) it returns…
jakubplus
  • 307
  • 4
  • 17
0
votes
3 answers

ident_current shows wrong last inserted id

I got a problem. I use SQL Server 2008 Express. When new row is inserted into a table the IDENT_CURRENT function returns correct value (last inserted row id). But when the last inserted row is deleted (DELETE FROM REPORT WHERE ID='last_inserted_id')…
jakentus
  • 896
  • 12
  • 22
0
votes
1 answer

Using select last_insert_id() from php not working

I have this exact same issue, but that answer is old, and I've read other places the use of mysql_insert_id() is depreciated and should be avoided in new code. The database is InnoDB and the primary key for this table is set to auto_increment. Can…
skkevinperson
  • 57
  • 1
  • 8
0
votes
1 answer

Inserting Persons with IDs in one query?

I need to add data to a MySQL database like that: Person: pId, nameId, titleId, age Name: nameId, name Title: titleId, title I don't want to have any names or title more then once in the db so I didn't see a solution with LAST_INSERT_ID() My…
user2170547
  • 361
  • 1
  • 3
  • 15
0
votes
2 answers

I'm using LAST_INSERT_ID() in php and mysql but i can't get a result. Why?

$sql_comp5 ="INSERT INTO `tiquets` (`Id_Tiquet`) VALUES (NULL); SELECT LAST_INSERT_ID()"; $result8 = mysql_query($sql_comp5); $flag_control=0; while ($row = mysql_fetch_assoc($result8, MYSQL_BOTH)) { …
jordi159
  • 7
  • 6
0
votes
1 answer

lastInsertId() not working on a PDO object, in an insert operation

I initialize the class with these options: $options = array( PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ); Then i behave like this $this->sql = trim($sql); $this->bind =…
axel
  • 3,778
  • 4
  • 45
  • 72
0
votes
1 answer

retrieving and using the auto-increment value WITHIN a query

My problem relates to retrieving and using the auto-increment value WITHIN a query I have three tables, tab1, tab2 and tab3. tab1 has a 1 to 1 relationship with tab3 (non-TNF I know, but necessary due to timing issues). tab2 has a many to one…
user2002907
0
votes
1 answer

Get ID right after it's set, on same page

The ThreadID in Thread_Body needs to be the id of Thread_Titles id. How would I go about doing this? if (isset($_POST['Post_Thread'])) { if (empty($_POST['Post_Title'])) { echo "No Title No Thread."; } else { $Sec =…
0
votes
1 answer

vb.net mysql Last_Insert_ID causing double DB entry?

I can't figure out why I am getting a double DB entry every time a subroutine is executed. I think I have an idea and I think it is something simple, but I'm not sure how to fix it. Thanks for any assistance anyone can provide. Here is my…
Jared Venema
  • 41
  • 2
  • 8
0
votes
1 answer

Is it possible to get a auto-generated values when using statement.getGeneratedKeys() in MySQL?

I am connecting mysql via java. I am trying to insert a record and i am finding the status using statement.getGeneratedKeys() and then if (generatedKeys.next()). Now can i get a value of coulmn which was inserted (ie) i have column named pass and it…
ACP
  • 34,682
  • 100
  • 231
  • 371
0
votes
1 answer

PHP MYSQL PDO lastInsertId is returning 0 when using a procedure to insert rows

I keep getting zero for LastInsertedID, I think it is because I am using a mysql stored procedure. Here is my code: // Set up a new MYSQL PDO Database Connection $dbConnection = new PDO('mysql:host=localhost;dbname=database;charset=UTF8',…
Kynrek
  • 1,319
  • 1
  • 10
  • 9