About global mysql connection on CI 4 |
Hello there,
I was using CI 3, but today i downloaded version 4. I could not set $db variable global to use every function? Anyone can give me example code ? Thanks... PHP Code: <?php namespace App\Controllers;
You are assigning $db and not $this->db. You should however set it in your BaseController.
PHP Code: class BaseController extends Controller (06-01-2020, 02:40 PM)jreklund Wrote: You are assigning $db and not $this->db. You should however set it in your BaseController. Thank you so much, it solved
It might be interesting to know that you can get a database connection instance anywhere without carrying it around in a base class property.
PHP Code: $db = \Config\Database::connect(); Sets $db to the default connection group. If the instance exists it will be returned, otherwise, a new instance is created and returned. Here's the signature for Database::connect() as found in /system/Database/Config.php PHP Code: public static function connect($group = null, bool $getShared = true) |
Welcome Guest, Not a member yet? Register Sign In |