CodeIgniter Forums
insert_id function bug - Interesting - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: insert_id function bug - Interesting (/showthread.php?tid=36141)



insert_id function bug - Interesting - El Forum - 11-23-2010

[eluser]pakistanvoices[/eluser]
hi buddies
I have separate database connection for "read" and "write", I have used insert_id() to get the last inserted id, it was working fine but in last few days, I was getting problem with my functionality, after tracking the code, I have found the insert_id function is giving the wrong value. It was really astonishing for me, any one have an idea about this problem or any one face this problem, then let me know.

My code:

Database Connection :
$this->DBr = $this->load->database('db_read', TRUE);
$this->DBw = $this->load->database('db_write', TRUE);

These are define in constructor of that particular model.

Function :
$this->DBw->insert_id(); // Giving the wrong value.


Best of Luck

Javaid


insert_id function bug - Interesting - El Forum - 11-23-2010

[eluser]WanWizard[/eluser]
Which database engine? Both databases running on the same engine (same IP and port number)?


insert_id function bug - Interesting - El Forum - 11-23-2010

[eluser]pakistanvoices[/eluser]
hi Lab Technician,
Thanks for taking interest in my problem, I am using the INODB + MYISAM and both connection method are running on the same database, there are no different databases right now.

Regards

Javaid


insert_id function bug - Interesting - El Forum - 11-23-2010

[eluser]WanWizard[/eluser]
The php mysql driver (i.e. not the CI code!) maintains an insert_id value per connection.

If both database instances use the same connection (which might be the case), insert_id is overwritten when you execute a second INSERT query via that same connection.


insert_id function bug - Interesting - El Forum - 11-23-2010

[eluser]pakistanvoices[/eluser]
hi
You are right, but it was working best from last one year, it creating a problem since 2 days, and there is nothing change in code.

Regards


insert_id function bug - Interesting - El Forum - 11-23-2010

[eluser]WanWizard[/eluser]
Behaviour usually doesn't change without reason. So there must be a change somewhere. If it's not in code, maybe in config (your app, php, mysql, the server, ...).


insert_id function bug - Interesting - El Forum - 11-23-2010

[eluser]pakistanvoices[/eluser]
Thanks buddy, I am tracking it and I ll sure tell you about my finding. Thanks again
Good Luck.