How do I get the last inserted ID of a MySQL table in PHP?
If you're using PDO, use PDO::lastInsertId
.
If you're using Mysqli, use mysqli::$insert_id
.
If you're still using Mysql:
Please, don't use
mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
But if you have to, use mysql_insert_id
.
Get last inserted id of any table in mysql
As long as auto_increment
is defined, that last inserted auto increment id can be retrieved from information_schema.tables
:
select IF(auto_increment = 1,
'No row has been inserted',
auto_increment - @@auto_increment_increment) As LastInsertedId
from information_schema.tables
where table_schema = 'DBName' and table_name = 'TableName';
How to get last id inserted. mysql_insert_id() not working
You need to use this in OpenCart for getting last insert id:
$this->db->getLastId()
From OpenCart User Guide:
Method Reference / DB::getLastId(): Returns the ID generated for an AUTO_INCREMENT
column by the previous query.
Getting the id of the last record inserted into a MySQL database
use an alias for get the value
$sql = "SELECT MAX(id) as max_id FROM tbl_uploads";
$result = $db_conx->query($sql);
$row = $result->fetch_assoc();
echo 'last_id: '.$row['max_id'];
How to Get last Insert Id in php without insert any data?
I hope this is what you're looking for
SELECT id FROM table ORDER BY id DESC LIMIT 1
Hope this helps you out
How do I get the last inserted id from a MySQL database table?
You can use mysql_insert_id();
function:
$query = "INSERT INTO Users ('fname','lname','email','username','password')
VALUES
('$fname','$lname','$email','$username','password')"
$lastid=mysql_insert_id(); /* GET THE ID OF THE INSERT QUERY */
$lastid++; /* INCREMENT THE ID */
Some tips for your future:
Use mysqli prepared statement instead of mysql. Read more about SQL injections here.
Retrieving the last insert id from MySql using PHP
There are 2 ways which you could solve this:
After inserting the entry in the sales_order:
Get the
last_insert_id
and store it in a php variable, then inject this value into the relevant queries.Store the
last_insert_id
in a MySql user-defined variable and use the variable in the query instead oflast_insert_id
.
sample code for option 2
SET @last_order_id = last_insert_id();
insert into sales_order_details (order_id, prod_id, prod_price)
values (@last_order_id, 5, 1500);`
insert into sales_invoice (order_id, invoice_id)
values (@last_order_id, 1);`
Related Topics
Is There a Use-Case For Singletons With Database Access in PHP
How to Properly Set Up a Pdo Connection
Is There a Function to Extract a 'Column' from an Array in PHP
Difference Between Public, Private, and Protected
Seo Friendly Url Results in CSS Img and Js Not Working
How to Create Friendly Url in PHP
Is "MySQLi_Real_Escape_String" Enough to Avoid SQL Injection or Other SQL Attacks
Allowed Memory Size of 33554432 Bytes Exhausted (Tried to Allocate 43148176 Bytes) in PHP
Difference Between Bindparam and Bindvalue
Detect Encoding and Make Everything Utf-8
Use an Array in a MySQLi Prepared Statement: 'Where .. In(..)' Query
How to Add Elements to an Empty Array in PHP
How to Send a Firebase Cloud Messaging Notification Without Use the Firebase Console