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

Created at: November 7, 2014; Last update: January 23, 2015

(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.

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:

to:

…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:

 

6 thoughts on “Step 3: Set up the database that will be used by CodeIgniter

    1. avenirer Post author

      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. 🙂

      Reply
      1. Salain

        Avenirer,
        Great tutorial!

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

        Reply
  1. Federico

    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

    Reply

Leave a Reply

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

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