Database query in a controller method? - 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: Database query in a controller method? (/showthread.php?tid=72985) |
Database query in a controller method? - happyape - 03-07-2019 Every now and then I need to run a quick database query which doesn't really belong to any of the Models. Or even when setting a quick demo site, I need a quick and dirty way. In CI3, I could just do Code: $this->db->query(); I thought of setting a private variable as below and then use it in any of the methods. I am wondering if there is any better way? PHP Code: class Home extends Controller RE: Database query in a controller method? - kilishan - 03-07-2019 What you did works perfectly fine. If you just need it in the one method, though, no need to set it at the class level. Code: $db = db_connect(); RE: Database query in a controller method? - happyape - 03-07-2019 Ok that's great to get that confirmation! Thank you @kilishan |