strange multiple database problem - 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: strange multiple database problem (/showthread.php?tid=43566) |
strange multiple database problem - El Forum - 07-16-2011 [eluser]applepear[/eluser] hi, there, trust me... I know there are many discussion on this topic and tried to look at older posts on this. here is my problem. i have two dbs, and here is how they are set up in database.php. Code: $active_group = 'userdb'; this example involves a few model classes. user model class uses the userdb, and a few other classes uses the knowledgedb. in user model class, DB is called by eg. Code: $this->db->get('user') in eg. knowledge model class, i have Code: function __construct(){ now in the controller, i have the following code: Code: function __construct(){ and so far, everything works well. then in one of the functions, i have: Code: function comment($kid) { the first call works fine, but the second call gives me a Database error as the following: Quote:Error Number: 1146 I think I did exactly as said in the user guide... how come the call to user model class is with the wrong DB if this is called after the $this->load->model('knowledge'...)? hope the story is told clearly... thanks to you all in advance. strange multiple database problem - El Forum - 07-16-2011 [eluser]Aken[/eluser] Assuming your user model is using the default database methods ($this->db->get(), etc), I would guess that when you load the knowledge database, it also sets the $this->db object database to the same, even though it's returning the object as well. Not custom tested, but it's something to look into. If you're using more than one database throughout your application, I'd recommend always assigning your databases to a separate variable like the user guide suggests, so you're always sure which one you're pulling from. strange multiple database problem - El Forum - 07-16-2011 [eluser]applepear[/eluser] I did the following experiment, and found that it seems that multiple database connection in CI does not work... here is my code: Code: $dbh1 = mysql_connect('localhost', 'root', ''); for the first part, I directly tested multiple db connection on my system, and it seems to be working just fine... echo result output from the two db back and forth without problem. then when it comes to CI, it will output the following error: Quote:A Database Error Occurred any idea? the DB configuration is the same as in the original post. strange multiple database problem - El Forum - 07-16-2011 [eluser]applepear[/eluser] I did a little digging in the above code: Quote:$dbh1: resource id = 34 type=mysql linkthey are all referring to different db, but dbh1 and dbh2 works, but db1 and db2 object from CI do not... strange multiple database problem - El Forum - 07-16-2011 [eluser]applepear[/eluser] problem seems to be pconnect... if multiple db connection is necessary, persistent connection does not work... set pconnect to false in db config, and it works. is this a problem limited to mysql db? strange multiple database problem - El Forum - 07-16-2011 [eluser]applepear[/eluser] quote from mysql_pconnect on php.org: Quote:mysql_pconnect() acts very much like mysql_connect() with two major differences. So, in mysql, when multiple persistent DB connections that share the same uname/passwd do not work. strange multiple database problem - El Forum - 02-20-2012 [eluser]broadband[/eluser] I tried quite the same code as you did. Codeigniter code just does not work. Code: <?php Above code returns: 1 1 I think that codeigniter does't change mysql_select_db when you use persistent connection. strange multiple database problem - El Forum - 02-20-2012 [eluser]broadband[/eluser] @returns 1 1, I meant that my current two databases have one row each. strange multiple database problem - El Forum - 02-20-2012 [eluser]InsiteFX[/eluser] If you go to the MySQL website and lookup persistent connection you see that even they do not recommend it. persistent connections are EVIL! |