Aakash Patel
Aakash Patel

Reputation: 11

Define constants from mysql database in PHP Codeigniter config file

Hi I am trying to defining constants from mysql database, but could not load the database in codeigniter config file.

$db =$CI->load->database();
$query = $db->get('constants');
foreach( $query->result() as $row ){ define($row->title, $row->value); }

But it is throwing me an error "Undefined variable: CI"

Upvotes: 0

Views: 3364

Answers (2)

user4419336
user4419336

Reputation:

At the bottom of the config/config.php, paste the following code.

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

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

$result = $query->result();

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

On Controller Test

echo $this->config->item('something');

Upvotes: 1

Brian Gottier
Brian Gottier

Reputation: 4582

To use the CI super object in a config file, you must use:

$CI =& get_instance();

Upvotes: 0

Related Questions