Introduction
CodeIgniter is an Application Development Framework; a toolkit. It has a small performance footprint due to the modular approach to loading its libraries and does a great job of separating logic from presentation using a Model-View-Controller (MVC) dynamic. CodeIgniter allows information to be retrieved, inserted, updated and deleted in your database with minimal scripting. In some cases only one or two lines of code is necessary to perform a database action.
Requirements
  • PHP Knowledge
  • PHP 5.3+ (5.4 Preferred)
  • MySQL
  • Apache (enabled mod_rewrite)
  • Or one of these setup: WAMP / XXAMP / MAMP / or LAM
When you need CodeIgniter
You can download at http://codeigniter.com/.
Retrieved Record
If you are using Codeigniter you can use SQL SELECT statements.
  1. $this->db-get(); // this function use to get all records into database .and this is by default function in codeigniter.
  2. $query = $this->db->get('database-table-name');
  3. // SELECT * FROM database-table-name'
  4. The second and third parameters enable you to set a limit and offset clause:
  5. $query = $this->db->get('database-table-name, 10, 20);
  6. // SELECT * FROM 'database-table-name LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)
  7. $query = $this->db->get(database-table-name);
  8. foreach ($query->result() as $row)
  9. {
  10. echo $row->field1;
  11. echo $row->field2;
  12. echo $row->field3;
  13. echo $row->field4;
  14. echo $row->field5;
  15. }
product
Insert data
You can either pass an array or an object to the function. Here is an example of using an array:
  1. $data = array(
  2. 'title' => 'My title' ,
  3. 'name' => 'My Name' ,
  4. 'date' => 'My date'
  5. );
  6. $this->db->insert(‘database-table-name’’, $data);
  7. // Produces: INSERT INTO database-table-name’ (title, name, date) VALUES ('My title', 'My name', 'My date')
Updating Data
You can pass an array or an object to the function. Here is an example of using an array:
  1. $data = array(
  2. 'title' => $title,
  3. 'name' => $name,
  4. 'date' => $date
  5. );
  6. $this->db->where('id', $id);
  7. $this->db->update(‘database-table-name’, $data);
  8. // Produces:
  9. // UPDATE mytable
  10. // SET title = '{$title}', name = '{$name}', date = '{$date}'
  11. // WHERE id = $id
Deleting Data
  1. $this->db->delete();
  2. Generates a delete SQL string and runs the query.
  3. $this->db->delete(‘database-table-name’, array('id' => $id));
  4. // Produces:
  5. // DELETE FROM mytable
  6. // WHERE id = $id
  7. The first parameter is the table name, the second is the where clause. You can also use the where() or or_where() functions instead of passing the data to the second parameter of the function:
  8. $this->db->where('id', $id);
  9. $this->db->delete(‘database-table-name’);
  10. // Produces:
  11. // DELETE FROM database-table-name
  12. // WHERE id = $id
  13. An array of table names can be passed into delete() if you would like to delete data from more than 1 table.
  14. $tables = array('table1', 'table2', 'table3');
  15. $this->db->where('id', '5');
  16. $this->db->delete($tables);