Retrieve, Insert, Update and Delete Functionality in Codeigniter – Info PHP

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

 

 

Retrieved Record

 

If you are using Codeigniter you can use SQL SELECT statements. 

  1. $this->db-get();   
  2. $query = $this->db->get(‘database-table-name’);  
  3.   
  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.   
  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.    
 

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.   

 

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->(‘database-table-name’, $data);  
  8.   
  9.   
  10.   
  11.   

 

 

 

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.   
  6.   
  7.   
  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.   
  12.   
  13.   
  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);  

Article Prepared by Ollala Corp

You might also like
Leave A Reply

Your email address will not be published.