Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get last id inserted. mysql_insert_id() not working

I am working in opencart version 2x. I am trying to get the last id of row inserted.This code working for 1.5x version but returning nothing in 2x

My code:

$this->db->query("INSERT INTO `" . DB_PREFIX . "endicia_label_info`  SET order_id  = '$orderID',postage_balance = '$postage_balance',file_name='$fileName',dte_created_date_time = NOW(),tracking_no='$tracking_no',final_postage = '$final_postage',mail_type='$Mailtype',mail_class='$ClassMail',label_size='$labelimgsize',label_pieces='$labelpieces',labelname='$labelname',Status='$Status'");
$label_id = mysql_insert_id();
return $label_id;
like image 479
Ammar Ul Hassan Avatar asked Mar 08 '16 09:03

Ammar Ul Hassan


Video Answer


2 Answers

Try with this method $this->db->getLastId();

like image 21
Maninderpreet Singh Avatar answered Sep 30 '22 14:09

Maninderpreet Singh


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.

like image 124
devpro Avatar answered Sep 30 '22 15:09

devpro