Welcome Guest, Not a member yet? Register   Sign In
About global mysql connection on CI 4
#1

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;

ini_set('display_errors',1);
class 
Home extends BaseController
{
    public function 
__construct()
    {

        
$db = \Config\Database::connect();
}
    public function 
index()
    {
        
$query $this->db->query('SELECT * FROM cats');
    
  $results $query->getResult();
print_r($results);
$data = [];
    
//    $data['cats'] = $results;
        
return view('index');

    }


    
//--------------------------------------------------------------------


Reply
#2

You are assigning $db and not $this->db. You should however set it in your BaseController.

PHP Code:
class BaseController extends Controller
{

    
/**
     * An array of helpers to be loaded automatically upon
     * class instantiation. These helpers will be available
     * to all other controllers that extend BaseController.
     *
     * @var array
     */
    
protected $helpers = [];

    
/**
     * Constructor.
     */
    
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        
// Do Not Edit This Line
        
parent::initController($request$response$logger);

        
//--------------------------------------------------------------------
        // Preload any models, libraries, etc, here.
        //--------------------------------------------------------------------
        // E.g.:
        
$this->db = \Config\Database::connect();
    }


Reply
#3

(06-01-2020, 02:40 PM)jreklund Wrote: You are assigning $db and not $this->db. You should however set it in your BaseController.

PHP Code:
class BaseController extends Controller
{

    
/**
     * An array of helpers to be loaded automatically upon
     * class instantiation. These helpers will be available
     * to all other controllers that extend BaseController.
     *
     * @var array
     */
    
protected $helpers = [];

    
/**
     * Constructor.
     */
    
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        
// Do Not Edit This Line
        
parent::initController($request$response$logger);

        
//--------------------------------------------------------------------
        // Preload any models, libraries, etc, here.
        //--------------------------------------------------------------------
        // E.g.:
        
$this->db = \Config\Database::connect();
    }




Thank you so much, it solved Smile
Reply
#4

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 nullbool $getShared true
Reply




Theme © iAndrew 2016 - Forum software by © MyBB