This question already has answers here:
How can I make sure that PDO's lastInsertId() is not that of another simultaneous insert?
(2 answers)
can I trust mysql_insert_id() to return correct value, multithreading paranoia
(2 answers)
Closed 8 years ago.
in PHP if I use
$last_id = $db->lastInsertId();
The variable $last_id does will get the value of the last row I've inserted in that instance or the latest row of the entire db?
if there are many users that connect and update database at the same time, the last id that I need in my application, will probably not fill the same as the latest of entire db.
Thanks in advance for response.
$last_id = $db->lastInsertId(); returns last inserted id for this DB conection.remember PDO will always return you the last ID inserted by the current active database connection.
Related
This question already has answers here:
mysqli_insert_id: What if someone inserts another row just before I call this?
(2 answers)
Closed 6 years ago.
is it safe to use mysqli_insert_id() when the database is heavily used and more than one id maybe inserted in the same time, or it will get only the id that inserted by a query from this user..
mysqli_insert_id() is specific to the database connection , it returns the ID of the row which you inserted most recently.
This question already has an answer here:
MySQL and the chance of the wrong id being returned by LAST_INSERT_ID()
(1 answer)
Closed 8 years ago.
I've a database with hundreds of records which inserted in each seconds, I have the below insert query and I want to know the insert id of this query:
$currQuery = $pdo->prepare("INSERT INTO some_table (...... )");
$currQuery->execute("....");
$queryInsertId = $pdo->lastInsertId();
I just want to know that is it safe to use lastInsertId() while I having hundreds of new records in every seconds? any ideas?
Yes it is safe. It returns the last insert id for the connection.
This question already has answers here:
PHP/MySQL insert row then get 'id'
(10 answers)
Closed 8 years ago.
I have two tables Production and Production_Detail. Production_Detail holds details of some order and has a foreign key to Production. When I insert a row into Production, auto-incrementing column sets the key for that row. I need that key so that it can be linked with the new Production_Detail rows that will be inserted after inserting the master row in Production.
Using php, I insert the data:
insert into Production Values ('','$producer_id','$order_date','$company_id','$emp_id');
I need to find that '' part so that it can be used for later quires.
In depends what mysql/query extension you're using, but PHP has a mysql_insert_id function to return the auto increment value for the most recently inserted row.
If you use PDO you can use the function
$id = $pdo->lastInsertId();
http://www.php.net/manual/en/function.mysql-insert-id.php
http://www.php.net/manual/en/mysqli.insert-id.php
Depending on how you are using the database you may have to pass the database variable.
mysql
$last_id = mysql_insert_id($db);
mysqli
$last_id = mysqli_insert_id($db);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: how to get last inserted ID of a table?
I use mysql_query() in PHP to insert a new record in my Database.
It will auto generate a row, with a user Id, can I use the result to get that Id?
mysql_insert_id()
Have you considered upgrading to PDO?
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there a way to return the id of a row that was just created in MySQL with PHP?
hi, i have an inset command such as
insert (name) values($name);
where i have id column as outoincrement how can i get the id of the inserted record after inserting directly
If you are working with mysql_* function, you'll have to use mysql_insert_id() (quoting) :
Retrieves the ID generated for an
AUTO_INCREMENT column by the
previous query (usually INSERT).
With mysqli, you'll use mysqli::insert_id().
And, with PDO, you'll call PDO::lastInsertId().
mysql_query("Your query here");
$last_id = mysql_insert_id();