Step 3: Set up the database that will be used by CodeIgniter

(created at: November 7, 2014; last update: January 23, 2015)

I won’t teach you how to create a database. You can use phpMyAdmin (or any other MySQL client for that matter) to do what you need. Also, you may have different connection parameters depending on your environment. One set of connection parameters for your development environment and another set for your live (production) server.

Once you’ve created the database(s), you must configure the CodeIgniter to allow the app to connect when needed.

For this we will use the database.php file that we moved in the previous step inside application/config/development/ folder. Also, if you’ve already set up databases for testing and production environments, you can copy database.php inside the coresponding directories.

Inside database.php we only have to make a few changes. You will find a $db array with a ‘default’ key. In it we will have to change the values corresponding to the keys: hostname, username, password, database.

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'password',
'database' => 'yourdatabase',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'autoinit' => TRUE,
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

Once we’ve done the changes, we should verify if all went well. To do that, we open the autoload.php file inside application/config, and we change the line:

$autoload['libraries'] = array();

to:

$autoload['libraries'] = array('database');

…and save the file. If something went wrong, when we visit our site we should see an error. If that happens, make sure you’ve set up correctly the database configuration.

Instead, if everything went well and you are greeted with the “Welcome to CodeIgniter!” message, you should – for the moment at least – stop “autoloading” the database library. For now we don’t need to connect to the database for any single request coming from the browser. So go back to your autoload.php and return to:

$autoload['libraries'] = array();

 

6 comments

    1. Why would you think that?… You should make a folder named “config” inside your application/development folder. I removed “ci” because it seems that it stumbled you. 🙂

      1. Avenirer,
        Great tutorial!

        The mistake is not the missing “ci” as Dijup mentioned it is “application/development/config” should be “application/config/development”

  1. Great tutorial Avenir, thanks for it.
    Just a question: I need to use two or more db into my application…I’m getting crazy trying to understand how to use two DB in the same time/script.
    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.