Welcome Guest, Not a member yet? Register   Sign In
insert_id() not returning
#1

[eluser]Clooner[/eluser]
Hello everybody.

I'm using CI for a while now and I really like it. I made some applications with it already. I'm working on a larger site right now and need to get the last inserted id. I am using active records but no id is returned. What is a possible problem? The data is inserted correct.

Code:
function AddAdres($data)
{
  $this->db->insert("adres", $data);        
  return $this->db->insert_id();
}

For the database I created a ID which is auto_increment and primary key.

I'm using CI 1.5.4, PHP 4.3.10 and mysql 4.1.9

Hope someone has an answer.

Thank you.
#2

[eluser]Clooner[/eluser]
I fixed the problem myself with this workaround. Somehow CI has a bug with returning the last inserted id. Hope this helps someone else

Code:
function AddAdres($data)
{
  $this->db->insert("adres", $data);
  $query = $this->db->query('SELECT LAST_INSERT_ID()');
  $row = $query->row_array();
  return $row['LAST_INSERT_ID()'];
}
#3

[eluser]maadmac[/eluser]
[quote author="clooner" date="1189010010"]I fixed the problem myself with this workaround. Somehow CI has a bug with returning the last inserted id. Hope this helps someone else[/quote]

Hi clooner,

Thanks for pointing this out, I noticed the same problem. Did you find documentation on this somewhere? Is it indeed a bug, or perhaps just something else we might have overlooked?

Thanks.
#4

[eluser]phester[/eluser]
The CI function works fine for me...

I am just a little curious how this behaves when you have more than one visitor at your site entering data at the same time. Will that query (or the CI insert_id() ) return the correct ID for the respective users?
#5

[eluser]maadmac[/eluser]
It should. Here's the line from the doc:

Quote:The insert ID number when performing database inserts.

Even with multiple connections, the data are still written sequentially, so you should get the correct ID for that operation.
#6

[eluser]Zelaza[/eluser]
Great work-around, clooner.

I did some research and it looks like the underlying mysqli_insert_id() function is always returning 0 - so this may be a PHP or MySQL problem, not a CodeIgniter problem.
#7

[eluser]tomodian[/eluser]
I had same problem, solved by this post. Thank you!
#8

[eluser]dizzu333[/eluser]
Ok, I solved my problem with plain SQL as in this post.
But shouldn't some one fix the incorrect behavior of db->insert_id() ?
#9

[eluser]Clooner[/eluser]
[quote author="dizzu333" date="1240841513"]Ok, I solved my problem with plain SQL as in this post.
But shouldn't some one fix the incorrect behavior of db->insert_id() ?[/quote]
Seems like the fault of php and not CI...
#10

[eluser]dizzu333[/eluser]
If I can retrieve the correct insert_id, couldn't CI do the same by using the insert_id() ?
If a function doesn't have the correct behavior on a certain PHP version or by using function(s) that return incorrect results, that means that functions has bugs. A function should return the correct result no matter what.




Theme © iAndrew 2016 - Forum software by © MyBB