Configuring CodeIgniter for databases

If you have already configured CodeIgniter to connect with a database, you can skip this part, as all we're going to do is make sure we can connect to a database; to do this, we're going to amend the following two files:

  • /path/to/codeigniter/application/config/database.php
  • /path/to/codeigniter/application/config/autoload.php

How to do it...

  1. In the database.php config file, look for the following lines and amend them accordingly:
    $db['default']['hostname'] = 'localhost'; 
    $db['default']['username'] = 'Replace with database username'; 
    $db['default']['password'] = 'Replace with database password'; 
    $db['default']['database'] = 'Replace with database name'; 

    The chances are that you'll not need to change $db['default']['hostname'] from the 'localhost', and replace other values (username, password, and database) with the specific values for your environment.

  2. In the autoload.php config file, look for the following line (around line 55):
    $autoload['libraries'] = array(); 
  3. Ensure that the database is being autoloaded by adding it to the $autoload array like this:
    $autoload['libraries'] = array('database'), 

    Tip

    Be sure to separate each library you're auto-loading with a comma, for example, $autoload['libraries'] = array('database', 'session', 'javascript') and so on.

How it works...

There's not a lot to this really; it's only setting the configuration settings, but one interesting point is that of autoloading the libraries. By placing a library name in this array in the autoload configuration file, you no longer need to load the library explicitly in a controller later in your application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.33.107