[SOLVED] Error on DB query: "Call to a member function result() on a non-object" |
[eluser]Byrro[/eluser]
Hello, I´m starting with CI and just can´t make this work: 1) I have two models ('client' and 'company') that are called in the same controller ('manager'). Code: // Model 'Client': (file: 'client.php' at models directory) /////////////////////////////////////////////////////////////////////////// Code: // Model 'Company': (file: 'company.php' at models directory) /////////////////////////////////////////////////////////////////////////// Code: // Controller 'Manager': (file: 'manager.php' at controllers directory) 2) The first model ('client') connects to the database 'clients' and the second model connects to another database called 'company_001'. 3) The problem is: the connection to the first database works just fine (tested), but I can´t work with the second model. It keeps giving me this error: Quote:Fatal error: Call to a member function result() on a non-object in (...) I´ve created a third DB to test with the second model, I´ve tried to connect to both BDs from the same Model, and got the same error! Could you please help me find where´s my mistake? Why isn´t it connecting to two databases?
[eluser]mddd[/eluser]
If you work with multiple databases, don't load them like you do now. Calling $this->load->database will create the $this->db object and that will be they way you call it. If you load multiple databases, you need to create different objects for them. See the manual page on connecting to a database and scroll down to 'connecting to multiple databases'. Addition: I'm glad you got it working but check this out anyway. You'll run into trouble if you initizalize the database, and later call a query on the other one. Or you'll have to load the right database before every query, but that's very inefficient. Better do something like this: Code: class Company extends Model {
[eluser]Byrro[/eluser]
Hello mddd, Thank you very much! The problem wasn´t with the underscore, but really with the connection to multiple databases. I considered it wasn´t necessary because I was working with different models. Your tip solved the trouble! Thank u again!
[eluser]Nairobi[/eluser]
Just wanted to say thanks - I am using CI2 with HMVC (first real CI project and with HMVC as well), and have several models being called from a controller. I was getting this exact issue - weird because the errors started showing up all of a sudden and I didn't make any db changes - and it appears that the $this-db call in each model was causing the error. Changed the db loading method for each model to give a $this->events_db and $this->jobs_db etc model specific db reference, and the problem disappeared. |
Welcome Guest, Not a member yet? Register Sign In |