Codeigniter changes database configuration at runtime

Is it possible to change the database configuration for each method in the controller?

$db['default']['db_debug'] = TRUE;

It is used by default TRUE, while I need to make it false in a specific method in order to catch an error and do something else (for example, show page 404).

When I tried $this->config->load('database'), it fails.

Another question:

Is it possible to check an invalid request and intercept it for some variables, and not display it for users other than setting db_debugto FALSE?

+5
source share
5 answers

I checked the system code / database / DB _Driver and found that:

$this->db->db_debug = FALSE;

, / .

+11

comenk, , .

-, Loader, MY_Loader.php

class MY_Loader extends CI_Loader
{
    function __construct()
    {
        parent::__construct();
    }

    /**
     * Load the Standard and/or Extended Database function & Driver class
     *
     * @access  public
     * @return  string
     */
    function database( $params = '', $return = FALSE, $active_record = NULL )
    {
        $ci =& get_instance();

        if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($ci->db) AND is_object($ci->db))
        {
            return FALSE;
        }

        $my_db      = config_item('subclass_prefix').'DB';
        $my_db_file = APPPATH.'core/'.$my_db.EXT;

        if(file_exists($my_db_file))
        {
            require_once($my_db_file);
        }
        else
        {
            require_once(BASEPATH.'database/DB'.EXT);
        }

        // Load the DB class
        $db =& DB($params, $active_record);

        $my_driver      = config_item('subclass_prefix').'DB_'.$db->dbdriver.'_driver';
        $my_driver_file = APPPATH.'core/'.$my_driver.EXT;

        if(file_exists($my_driver_file))
        {
            require_once($my_driver_file);
            $db = new $my_driver(get_object_vars($db));
        }

        if ($return === TRUE)
        {
            return $db;
        }

        // Initialize the db variable.  Needed to prevent
        // reference errors with some configurations
        $ci->db = '';
        $ci->db = $db;
    }
}

, MY_DB_mysqli_driver.php, mysqli , CI database.php.

comenk MY_DB_mysqli_driver.php

function debug_on() {
     return $this->db_debug = TRUE;
}

function debug_off() {
     return $this->db_debug = FALSE;
}

function in_error() {
     return (bool) $this->_error_number();
}

/

$this->db->debug_off();

$this->db->query('SELECT * FROM `table`');

if( $this->db->in_error() ) {
    show_404();
}

$this->db->debug_on();
+3

system/database/DB_driver.php

function debug_on()
{
     $this->db_debug = TRUE;
     return TRUE;
}

function debug_off()
{
     $this->db_debug = FALSE;
     return FALSE;
}

$this->db->debug_off();
$this->db->reconnect();
+1

$this- > db- > db_debug = 0;//0: off, 1: on ... $GLOBALS, .

+1

SQL ( ), php. CodeIgniter .

index.php

error_reporting(E_ALL);

error_reporting(0);

. , CI. , .

To debug SQL, you can create a class that inherits from CI_Model, and then create all the model classes to extend this class. In this class, you can add code to run queries that write requests to the log so that they are easier to debug. This will not help if the request itself is bad, but you must understand this before you reach this point.

0
source

All Articles