# 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

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

• How to find a name, ID, email...

// Select any row where the field title = $title and the field status =$status
$array = array('name' =>$name, 'title' => $title, 'status' =>$status);
$this->db->where($array);
$query =$this->db->get('mytable');
$result =$query->result_array();
• How to insert data

// Insert a new row in the table
$data = array('name' => Paris, 'title' => Amour, 'Date' => 1998);$this->db->insert('mytable',$data);  • How to update data // Update a given row in the table$this->db->set('field','field+1');
$this->db->where('id',4);$this->db->update('mytable');
• How to order data

// Order titles in increasing order
$this->db->order_by('title','ASC'); // Order titles in descending order$this->db->order_by('title','DESC');
// Order titles randomly
$this->db->order_by('title','RANDOM');  • How to delete data // Delete the data where id=$id from the table mytable
$this->db->delete('mytable', array('id'=>$id);
// Delete an entire table
\$this->db->empty_table('mytable');              

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