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

Sql Query using multiple inserts and multiple LAST_INSERT_ID() 's is not working

Hi guys I'm trying to store questions & answers in 2 tables using 1 sql query: INSERT INTO questions ( question, level_ID ) VALUES ( 'hello', '1'); INSERT INTO answers (questions_ID, answer, ok) VALUES (LAST_INSERT_ID(),'doei',0),…
Arjo Post
  • 23
  • 5
1
vote
2 answers

SQLite3 and last_insert_rowid

This is how I create my handle $this->_handle = new SQLite3($this->_dbname); This is how I make my db query (shortened): $stmt = $this->_handle->prepare($sql); // execute query $result = $stmt->execute(); // get all results while($row =…
testing
  • 19,681
  • 50
  • 236
  • 417
1
vote
1 answer

How to return row id after copying row in MySQL table?

I need to copy/duplicate a row in my table and then return the new row's 'id' value. My 'id' column is an auto-increment field. $sth = $dbh->prepare("DROP TEMPORARY TABLE IF EXISTS tmp_users; CREATE TEMPORARY TABLE tmp_users …
Michael
  • 2,276
  • 15
  • 49
  • 80
1
vote
2 answers

PHP for a single MySQL multi-row INSERT with LAST_INSERT_ROW for each row?

Skip to "My Code" at the bottom of the question to get straight to the point. $con = mysql_connect('localhost', 'mysql_user', 'mysql_password'); General Examples: Multiple query strings can be executed within a single mysql_query, like…
Kirkland
  • 2,319
  • 4
  • 20
  • 27
1
vote
1 answer

stored procedures and mysql_insert_id issue

My problem is this: I Created a store procedure from php to mysql. Now before someone says something about that practice, The procedure works fine. The problem is this, when I call the procedure from php and I KNOW that a record has been entered,…
Not Amused
  • 942
  • 2
  • 10
  • 28
1
vote
4 answers

Mysql use LAST_INSERT_ID in stored procedure

I need to create a table based on the last id inserted into another main table, for this I created the following stored procedure: CREATE PROCEDURE `sp_create_campaign` ( IN p_vName VARCHAR(70), IN p_iIdOper INT(11), …
csotelo
  • 1,453
  • 2
  • 28
  • 43
1
vote
3 answers

How can I retrieve a last_insert_id from a specific query in a transaction?

I have a mysql transaction with three queries In the third query I am trying to pull last_insert_id values from the two previous queries. VALUES ('".$result1[last_id]."','".$result2[last_id]."')"; But it doesn't work. Any suggestions?
Richard
  • 209
  • 2
  • 10
1
vote
1 answer

PHP PDO::lastInsertId() and ATTR_PERSISTENT

I have almost the same question as this. Based on that post I know that PDO::lastInsertId() is safe when called from different connections. However, does this mean PDO::ATTR_PERSISTENT => true should not be used in the PDO constructor if you want to…
Rob Johansen
  • 5,076
  • 10
  • 40
  • 72
1
vote
1 answer

flex local database lastInsertId = 0

var insertReq:SQLStatement = new SQLStatement(); insertReq.addEventListener(SQLErrorEvent.ERROR, dbErrorHandler); insertReq.sqlConnection = conn; insertReq.text = "INSERT INTO table1 (inputTime) VALUES…
vicx
  • 21
  • 1
  • 6
1
vote
2 answers

MySQL insert from a table assigning auto-increment ID and update FK in third table

I have 3 tables old_customers id name 5 Mario 13 John .. ... new_customers id name address 7 Luigi Roma .. ... ... orders id customer_id 1 5 2 7 3 13 .. .. I want to copy old_customers to new_customers assigning…
Salvador
  • 786
  • 8
  • 21
1
vote
2 answers

MYSQL: stored procedure, insert a row and then select it via LAST_INSERT_ID()

I am trying to make a stored procedure call AddCluster witch is taking to parameter 'title' and 'alt' Case 1: If 'title' is in the db then just return the "old" row! Case 2: If the 'title' is NOT in the db then, insert a row base on parmerer 'title'…
Voidcode
  • 1,229
  • 1
  • 18
  • 22
0
votes
1 answer

C# mysql one return last_insert_id

I am trying to create a method in which I can exequte mysql UPDATE, DELETE or INSERT query. The method must work when with an INSERT I ask or do not ask the last_insert_id(). Below is the code that I have at the moment: public int…
Bernhard
  • 1,455
  • 5
  • 19
  • 24
0
votes
3 answers

Why can't I get the last insert ID in PHP and MySQL?

I have a function that creates a course. I am trying to get the Last Insert ID but it doesn't work: public function createCourse() { require "/mysqli_connect.php"; $course_q = "INSERT INTO course (....) VALUES (.....)"; $course_r =…
Johnathan Au
  • 5,244
  • 18
  • 70
  • 128
0
votes
2 answers

How to return variables that have been set in MySQL back to PHP

I have a PHP function which inserts multiple records into MySQL: function commit_purchase($asset_type_ID, $org_ID, $asset_desc, $asset_cost, $date, $org_to_member_ID, $asset_ID, $purchaser_cur_invest, $purchaser_cred_deb, $purchaser_balance) { …
Progger
  • 2,266
  • 4
  • 27
  • 51
0
votes
2 answers

How do I use LAST_INSERT_ID() in WebMatrix

I'm having trouble figuring out how to use LAST_INSERT_ID() for a MySQL database in WebMatrix. Can someone show me how to apply it to the code snippet below? var clothingsize=""; if(IsPost){ clothingsize =Request["clothingsize"]; var…
Chaya Cooper
  • 2,566
  • 2
  • 38
  • 67