Well... I'm out of ideas on what to write about. Do you have any ideas? Please, write me in any comment section you find, and I will do my best to tell you about that particular subject

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.

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

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

PHP

1

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

to:

PHP

1

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