0

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"

1
  • I don't think so that we can do DB operation in config file ? Commented Sep 15, 2017 at 1:44

2 Answers 2

1

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');
Sign up to request clarification or add additional context in comments.

1 Comment

I upvoted for a creative answer, but this is obviously a bandage for what I suspect is a broken app architecture. You have to be honest, this has Kludge written all over it. Even though CodeIgniter provides all kinds of ways to do things right, there's this ...
0

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

$CI =& get_instance();

2 Comments

Not working, throwing an error Fatal error: Uncaught Error: Class 'CI_Controller' not found in C:\xampp\htdocs\xampp\CodeIgniter\system\core\CodeIgniter.php
This is the only way to obtain the CI super object in a config file or library. Are you loading the config file with $this->config->load() ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.