简体   繁体   中英

Codeigniter $this->db->insert_id() returning 0 on mysqli but $this->db->query('SELECT LAST_INSERT_ID()') works

This is my database config:-

$db['default'] = array(
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'test_db',
        'dbdriver' => 'mysqli',
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => TRUE,
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
    );

I insert user using this and output last id on die();

$this->db->insert("user",$data);
$lastid=$this->db->insert_id();
die($lastid);`

It outputted 0

But when I use:-

$query = $this->db->query('SELECT LAST_INSERT_ID()');
$row = $query->row_array();
$lastid= $row['LAST_INSERT_ID()'];
die($lastid);

It outputted the last inserted id...

I have an auto_increment on user_id

Why $this->db->insert_id() wont work?

Based on the link what i have given to you (in comments):-

1.From the first link( $this->db->insert_id(); returning 0 every time in codeigniter ) You may be don't have an primary-key auto-incremented column. So you can then use:-

$this->db->affected_rows()

(But this will not give you the last inserted id, it will tell you how many rows are affected because of insert query)

2.From the second link( https://kedyr.wordpress.com/2012/10/03/codeigniter-insert_id/ ) exactly the same solution is given what you have tried in your second attempt(which is working for you),but there is no explanation given why this behavior is occurring.

3.based on :- http://forum.codeigniter.com/thread-61881.html

MySQLi resets the insert id upon committing the transaction (intended behaviour). When it's without a transaction, I think that behaviour differed between PHP versions, due to a bug. Try wrapping it in a transaction, by calling trans_begin(), then the insert(), then the insert_id(), then commit().

But i think that is a very high-end concept (even i have no idea how to perform this, so no comments).

4.from this link:- https://github.com/bcit-ci/CodeIgniter/issues/591 Again it is saying:-

a. If there was no auto_increment column it would return 0

b.No mysqli connection/link id would return false(which is actually 0 in php).

Lastly once try with this:-

$this->db->insert("user",$data);
$lastid=$this->db->insert_id();
echo $lastid;die;

I have checked and it works fine.Please try it like this

public function addNewAdmin($data) {

        $result = $this->db->insert("admins", $data);
        $lastid=$this->db->insert_id();        
        echo $lastid;exit;        
}

$this->db->insert_id() returns the id of the last row where the insert was made, Making sure you have set you id column as the primary key to auto increment. use this;

$this->db->insert('table',$array(''=>,));
$id=$this->db->insert_id();`enter code here`
return $id;

I think the id is not being returned because your auto incremental value is not just plain ID but user_id.. this is because I tried

$this->db->insert("user",$data);
$lastid=$this->db->insert_id();
die($lastid);

With a table which had ID and it worked but when I tried it with a table with item_id in the same project that did not work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM