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
Related
This question already has answers here:
Best way to do multi-row insert in Oracle?
(9 answers)
Closed 3 years ago.
In order to run a query for database MySQL, we are using mysqli_query. Then, for running a query for database Oracle, we are using oci_execute.
When we wish to running multiple query for database MySQL, we are using mysqli_multi_query. Example as below:-
$mysqliconn = mysqliconn(); //mysqli connection
$sql = '
INSERT INTO TABLE VALUES();
INSERT INTO TABLE VALUES();
INSERT INTO TABLE VALUES();
INSERT INTO TABLE VALUES();
';
if(mysqli_multi_query($mysqliconn, $sql)) {
echo 'Success';
}
My Question is if there anyone of you whom can come out with the most simplest solution to run multiple inserting values into the database table using one command execution.
Oracle has an INSERT ALL statement for that.
Otherwise you could just loop in your code and execute it n times.
$sql = '
INSERT INTO TABLE abc VALUES(123,'xyz'),(456,'def'),(789,'qwe');
';
I suggest that using support library ^^
This question already has answers here:
PHP/MySQL insert row then get 'id'
(10 answers)
Closed 9 years ago.
I am inserting record into database using the insert query as
INSERT INTO task_record(assigned_by,handle_by) VALUES('$assigned_by', '$handle_by')
adding this record assign a unique ID to the each record i want to get that ID as soon as the record is inserted.
Is there any simple way to modify the query or i have to use one more query of select which is not a fine option
If your interfacing with mysql directly execute SELECT LAST_INSERT_ID() after the insert query.
with the now obsolote mysql extension you could execute mysql_insert_id();
with the mysql improved extension use mysqli_insert_id();
Try with $this->db->insert_id() like
echo "The Last Insert Id is ".$this->db->insert_id();
Or like
echo "The Last Insert Id is ".$this->insert_id();
Or even simply
echo "The Last Insert Id is ".mysql_insert_id();
But try to avoid using mysql_* functions due to they are deprecated.Instead use either mysqli_* functions or PDO statements.
EDIT:
$query = "SELECT LAST_INSERT_ID() task_id";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$id = $row['task_id'];
}
echo $id;
This question already has answers here:
PHP/MySQL insert row then get 'id'
(10 answers)
Closed 9 years ago.
I'm running a MySQL INSERT query where I insert some data into the database.
Each row has a unique ID generated each time a new entry is added. I'd like to get this ID straight after I insert the data.
PHP Code:
$addGiveawayToDb = mysql_query("INSERT INTO $table(orientation, title, color)
VALUES ('$orientation', '$title', '$color')")
or die(mysql_error());
//Here I need to get the row id of the above data...
Thanks!
Try like
$addGiveawayToDb = mysql_query("INSERT INTO $table(orientation, title, color)
VALUES ('$orientation', '$title', '$color')")
or die(mysql_error());
echo "Inserted Id is ".mysql_insert_id();
Makesure that you have an auto increment field in your table.And also try to avoid mysql_* functions due to they are deprecated.Instead use mysqli_* functions or PDO statements.
See this LINK
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 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();