2

I am new to Codeigniter..

So this is my table:

request_id | login_name | login_password | reg_date | status

Can I use $this->db->insert_id() to get last inserted id if the primary auto increment column name is request_id instead of id?

Any help will be appreciated..

Arun Unnikrishnan
  • 2,339
  • 2
  • 25
  • 39

3 Answers3

6

Yes, insert_id in MySQLi does not care about the column name, just that the column is AUTO_INCREMENT. It returns;

The value of the AUTO_INCREMENT field that was updated by the previous query. Returns zero if there was no previous query on the connection or if the query did not update an AUTO_INCREMENT value.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
1

Yes you can use.

$this->db->insert_id() gives you id of last inserted record, so no need to change the method name.

refer here

Sujit Singh
  • 921
  • 1
  • 10
  • 20
1

Yes using $this->db->insert_id() you can get the id of last inserted record in table.
Like this :

     <?php    
    class InsertRecord extends CI_Model {
    var $tablename = "test_table";
    var $primaryID = "request_id";

        function insertRecord(){
             $insertArr['login_name'] = $_POST['login_name'];
             $insertArr['login_password'] = $_POST['login_password'];
             $insertArr['reg_date'] = $_POST['reg_date'];       
             $insertArr['status'] = $_POST['status'];
             if ($this->db->insert($this->tablename, $insertArr)) {
                $lastInsertedID = $this->db->insert_id();
             }
        }
     }
    ?>

In $lastInsertedID variable you will get id of last inserted record.

Hope this will help you... :)

Yogesh Pingle
  • 3,545
  • 3
  • 16
  • 16