Do me a favor, take a few seconds to have a look at my last project.

Thank you, Lulu

How to perform basic DataBase operations with CodeIgniter?

Configuration of the Database

First, you have to configure the Database (DB) in the folder /application/config/database.php. In this file, you will be able to set everything about the database. The main lines you would have to configure are the first five in the following code:

<?php
$db['default'] = array(
1   'dsn'      => 'mysql:host=localhost; dbname=domainname; charset=utf8',//Describe the connection to the DB
2   'hostname' => 'localhost',             //The hostname of your server
3   'username' => 'name',                  //Users who can access to the DB
4   'password' => 'pass',                  //Password required for accessing to the DB
5   'database' => 'dbname',                //Name of the DB
6   'dbdriver' => 'pdo',                   //Driver of the DB
7   'dbprefix' => '',                      //Optionnal, prefix for a table name
8   'pconnect' => FALSE,                   //Whether to use a persistant connection
9   'db_debug' => (ENVIRONMENT !== 'production'),  //How to display errors (environment)
10  'cache_on' => FALSE,                   //Enable/Disable query caching
11  'cachedir' => '',                      //Location of the cache
12  'char_set' => 'utf8',                  //The character set used in communication with the DB
13  'dbcollat' => 'utf8_general_ci',       //The character collation used in communicating with the DB
14  'swap_pre' => '',                      //A default table prefix that can be swapped with the dbprefix
15  'encrypt'  => FALSE,                   //Whether or not to use an encrypted connection
16  'compress' => FALSE,                   //Whether or not to use client compression (MySQL only)
17  'stricton' => FALSE,                   //TRUE/FALSE - forces 'Strict Mode' connections
18  'failover' => array(),                 //An array with data in case the connection to the main db fails
19  'save_queries' => TRUE                 //TRUE/FALSE - Whether to "save" all executed queries.
); 
?>

Connection to the Database

There is two way to make a connection to a database. It can be done manually or automatically.

How to make queries ?

Every functions you could use to manipulate the database can be founded on this page. To make a summary, I will quote the main functions you could use:

A lot of other functions are available : like(), count_all_results(), select_avg or limit().

See also


Last update : 04/13/2019