Retrieve, Insert, Update and Delete Functionality in Codeigniter

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. }  
  16.    
 product
Insert data
 
You can either pass an array or an object to the function. Here is an example of using an array:
  1.    
  2. $data = array(  
  3. 'title' => 'My title' ,  
  4. 'name' => 'My Name' ,  
  5. 'date' => 'My date'  
  6. );  
  7. $this->db->insert(‘database-table-name’’, $data);  
  8. // 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.    
  2. $this->db->delete();  
  3. Generates a delete SQL string and runs the query.  
  4. $this->db->delete(‘database-table-name’, array('id' => $id));  
  5. // Produces:  
  6. // DELETE FROM mytable  
  7. // WHERE id = $id  
  8. 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:  
  9. $this->db->where('id'$id);  
  10. $this->db->delete(‘database-table-name’);  
  11. // Produces:  
  12. // DELETE FROM database-table-name  
  13. // WHERE id = $id  
  14. An array of table names can be passed into delete() if you would like to delete data from more than 1 table.  
  15. $tables = array('table1''table2''table3');  
  16. $this->db->where('id''5');  
  17. $this->db->delete($tables);  

Up Next
    Ebook Download
    View all
    Learn
    View all