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);
Related
This question already has answers here:
How do I get the last inserted ID of a MySQL table in PHP?
(16 answers)
Closed 7 years ago.
hi ther I use this mysqli code to insert in DB.
mail DB has an id AUTO_INCREMENT.
$sql = "INSERT INTO mail (".$sqlname.") VALUES (".$sqlValue.")";
if (!($conn->query($sql) === TRUE))
{
echo 'false';
return false;
}
else
return $id; // return id of insereted record in table
Is there any way to return id in mail table realtime after insert?
I can call last ID by call another function....But it may gone wrong for high traffick insert to DB.
Description:
When a new AUTO_INCREMENT value has been generated, you can also obtain it by executing a SELECT LAST_INSERT_ID() statement with mysql_query() and retrieving the value from the result set returned by the statement.
For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client. It is not even changed if you update another AUTO_INCREMENT column with a nonmagic value (that is, a value that is not NULL and not 0). Using LAST_INSERT_ID() and AUTO_INCREMENT columns simultaneously from multiple clients is perfectly valid. Each client will receive the last inserted ID for the last statement that client executed.
See detail here: https://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
If you are using mysqli, you can do it with mysqli_insert_id:
return $conn->insert_id;
If you are using PDO, you can do it with lastInsertId:
return $conn->lastInsertId();
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.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
mySQL return index after insert
Hy,
Is there a way that after inserting something in mysql database (INSERT INTO table_name VALUES () ...) to retrieve ID of that insert for further manipulation ?
The old way is to make the insert and then query a SELECT on the table name . But is there any way to do this in a single query ?
Thank you very much
In PHP, call mysql_insert_id(). Or directly in MySQL, call the LAST_INSERT_ID() function.
// PHP
echo mysql_insert_id();
// PHP MySQLi
echo $mysqli->insert_id;
-- in MySQL
SELECT LAST_INSERT_ID();
you have a function called
$lastId = mysql_insert_id();
for more info: http://php.net/manual/en/function.mysql-insert-id.php
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();
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: how to get last inserted ID of a table?
I'm writing a function to add data do a database, but I then want to immediately use that entry in my code.
I know I could query: SELECT id FROM table ORDER BY id DESC
But I wonder if there's any more direct way of doing it.
That mostly depends on the interface you're using to access MySQL.
If you're using PDO (recommended), you'd use PDO::lastInsertId(): http://us.php.net/manual/en/pdo.lastinsertid.php
If you're using MySQL, you'd use mysql_insert_id() http://php.net/manual/en/function.mysql-insert-id.php
If you're using MySQLi, you'd use mysqli_insert_id(): http://us.php.net/manual/en/mysqli.insert-id.php
Just simply call whichever function is appropriate right after your INSERT completes.
It's important to note that you likely do not want to fetch id out of the database using SELECT because if you have multiple writers, you may get the id that some other thread inserted. At best, you'll operate on the wrong data, and at worst, you could have serious security issues and/or corruption.
With PHP you can use mysql_insert_id() function.
select last_insert_id();
Doc
Maybe this could help:
Get the Unique ID for the Last Inserted Row
If you insert a record into a table
that contains an AUTO_INCREMENT
column, you can obtain the value
stored into that column by calling the
mysql_insert_id() function.