Skip to content
Advertisement

Can we fire a database query in config/constants.php file to make constants dynamic? (Php Codeignitor)

I have tried to place this at the bottom of the config/config.php

require_once( BASEPATH .'database/DB.php');
$db =& DB();

$query = $db->get('constants');
$result = $query->result();

foreach( $result as $row)
{
    $config[$row->title] = $row->value;
}

but it is throwing this error: An uncaught Exception was encountered

Type: Error Message: Class ‘CI_Controller’ not found Filename: E:xampp74htdocsjrvssystemcoreCodeIgniter.php Line Number: 369

Please suggest me some solution for this. If this is not possible then what else we can do to make constants dynamic?

Advertisement

Answer

constants.php is the very first file that is loaded in Codeigniter.php so you can’t use the instance of codeigniter in this file cause if hasn’t been loaded yet and of course you can’t rely on hooks because it loads after this file has been loaded and constants had been defined so!

You best option is to manually connect to your db in top of this file or in index.php and query what you need then use the values in define.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement