![]() |
ErrorException Undefined variable: db - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: ErrorException Undefined variable: db (/showthread.php?tid=79573) |
ErrorException Undefined variable: db - lordmight - 07-04-2021 I have been using CodeIgniter since version 2 and been using version 3 until this day. I would like to try CodeIgniter 4 but I can't seem to load the database library, even if the database is defined on my BaseController: Here's the BaseController PHP Code: <?php and this is my Controller PHP Code: <?php Whenever I try to go to Landing controller it gives ErrorException that $db is undefined. But if I declare the $db = \Config\Database::connect(); on my Landing controller it works. I just want to make it global. RE: ErrorException Undefined variable: db - includebeer - 07-04-2021 $db doesn't exists, use $this->db to access the variable from your base controller. Also, don't try to use CI4 the "CI3 way". There's no more global object linking to everything like there was in CI3. The model class already have a reference to the db object, so there's no need to load it in the base controller and keep a reference in memory. RE: ErrorException Undefined variable: db - ikesela - 07-04-2021 db is a shared instances. $db = db_connect() from model class , can access thru : $this->db RE: ErrorException Undefined variable: db - InsiteFX - 07-05-2021 If you use the model the db connection is already made. |