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
1
vote
2 answers

MySQL Function with LAST_INSERT_ID() giving unexpected results

I have spent 24 hours on this and need to leverage existing knowledge for speedsake. I have this mysql function: DELIMITER $$ USE `db`$$ DROP FUNCTION IF EXISTS `insertindb`$$ CREATE DEFINER=`root`@`localhost` FUNCTION `insertindb`(itemtype …
user1729972
  • 712
  • 2
  • 9
  • 29
1
vote
1 answer

How to avoid a possible conflict associating data sent by a form into a MySQL database with an autoincrement unique id?

I do not actually know much about PHP and jQuery. I am a self-taught programmer, lately I've focused on web programming with PHP for the server-side and JavaScript for client-side. AJAX is a relatively new concept for me, I'm just doing the first…
candlejack
  • 1,189
  • 2
  • 22
  • 51
1
vote
2 answers

SQL Insert Row and copy inserted Auto-Increment id to another column

I am using PHP and MySQL. Goal: Insert a new row into a table with an auto-increment id column. While inserting this new row, I would like to copy the new inserted row's auto-increment id # into another column in the same row/entry. Upon creation,…
agent provocateur
  • 824
  • 3
  • 19
  • 39
1
vote
1 answer

MySQL INSERT and SELECT LAST_INSERT_ID() in one line

I was wondering if there was a way to change this statement INSERT INTO [table] VALUES ([values]); SET @last_id = LAST_INSERT_ID(); Into a single line. Something such as SELECT CASE WHEN ( SELECT COUNT(id) FROM [table] WHERE id=1 ) > 0 THEN…
RonnyKnoxville
  • 6,166
  • 10
  • 46
  • 75
1
vote
1 answer

How to get the last insert ids from laravel 4

I am using laravel 4. I have a table called documents. I am inserting data in the Document table using Document::insert() function. My code is given below $statuses = array(); foreach($content as $i){ array_push($statuses, array( …
odbhut.shei.chhele
  • 5,834
  • 16
  • 69
  • 109
1
vote
0 answers

MySQL :: insert_id returning non zero value for a SELECT query

I've just spent the last 4 hours tracing a bug in my script. I kept receiving the following error: Call to a member function fetch_assoc on a non object This was being thrown when I did something like: $query = $database -> query("SELECT…
Keir Simmons
  • 1,634
  • 7
  • 21
  • 37
1
vote
1 answer

MySQL - Return group of last inserted ID's

So theres lots of questions and answers about LAST_INSERT_ID(). This works fine if you're inserting a single row and need the return value of that ID to do stuff with, but what if you are doing a group insert and need the return values of all…
Jamie S
  • 760
  • 3
  • 9
  • 19
1
vote
1 answer

MySQL syntax for UPDATE + CASE WHEN + EXISTS + SELECT MAX

EDIT: Working now, solution in the bottom of this post. I have tried to create this query for hours now, without success: UPDATE tasks SET `Order`= ( CASE WHEN ( SELECT EXISTS ( SELECT 1 FROM…
bloodleh
  • 493
  • 8
  • 28
1
vote
1 answer

How to get last Insert Id while logging the query using PHP-PDO

Recently I have been creating a function to record every changes in the database, simply I record it to a table. Table backup_log: backupTableName, backupLastUpdate, backupLastupload So, every time changed, its will record the date to "backup_log".…
1
vote
1 answer

What do "affected rows" and "last_insert_id" in mysql sever & client protocol used for?

As seeing to the mysql server and client protocol , I find the two columns "affected rows" and "last-insert-id" in Ok_Packet. I know the Ok_packet is sent by server to the client as a response of commands sended by a client. It seems useless for the…
Windfell
  • 38
  • 6
1
vote
1 answer

insert new row if not exist and get new or existing id

I want to do the following by using NOT EXISTS and last_insert_id() by one code. below code satisfies my needs, Col1 is a unique key there for code will not insert data if it already exists. If a new row is inserted or is already exist always code…
user3025146
  • 19
  • 1
  • 5
1
vote
1 answer

MySQL Last_Insert_ID Syntax

I have this code: //insert user input into db $query = "INSERT INTO test_details (test_title, user_id, likes) VALUES ('$title', '$user_id', '0')"; $query .= "INSERT INTO test_descriptions (test_id, description) VALUES (LAST_INSERT_ID(),…
Tom
  • 167
  • 9
1
vote
1 answer

SELECT LAST_INSERT_ID() Not working with BIGINT on MySQL 5.6.11 When Using a Trigger

I'm posting a new question following my previous post "SELECT LAST_INSERT_ID() Not working with BIGINT on MySQL 5.6.11" In any case, after further investigation I found that the LAST_INSERT_ID() was not working because I had a trigger BeforeInsert…
neildt
  • 5,101
  • 10
  • 56
  • 107
1
vote
0 answers

SELECT LAST_INSERT_ID() Not working with BIGINT on MySQL 5.6.11

I have a BIGINT field as an auto increment and primary key on a MySQL Innodb table, running MySQL Community Server 5.6.11. After calling a basic INSERT statement, and then calling SELECT LAST_INSERT_ID(), I'm always returned 0, even though the…
neildt
  • 5,101
  • 10
  • 56
  • 107
1
vote
1 answer

PDO::lastInsertID sometimes doesnt work

Something strange is happening with my pdo connection when I get the lastInsertId(): in 90% times it work well, but in 10% of the cases it returns 0 (I don't know if is really 0 or FALSE). But in 100% of cases my records are successfully…
pzz
  • 11
  • 1