PHP
  Home arrow PHP arrow Page 4 - Inserting, Updating and Deleting Database Rows with Code Igniter
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Inserting, Updating and Deleting Database Rows with Code Igniter
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2008-09-24


    Table of Contents:
  • Inserting, Updating and Deleting Database Rows with Code Igniter
  • Performing conditional SELECT statements with Code Igniter
  • Inserting new rows into a selected MySQL table
  • Updating and deleting database records using the active record pattern

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Inserting, Updating and Deleting Database Rows with Code Igniter - Updating and deleting database records using the active record pattern
    ( Page 4 of 4 )

    True to form, updating and deleting database rows with the active record pattern is only a matter of using a couple of intuitive methods, called “update()” and “delete()” respectively. They can be included in a simple controller class, as I did before when explaining how to perform database insertions.

    To dissipate any possible doubts about how to use these method, I coded two controllers. The first one updates an existing row of a sample MySQL table, and the second one deletes a specified record.

    Here are the respective controllers, so you can examine them in detail:

    class Users extends Controller{

    function Users(){

    // load controller parent

    parent::Controller();

    // load database class and connect to MySQL

    $this->load->database();

    }

    // update row of 'users' MySQL table

    function index(){

    $data=array('firstname'=>'Jane','lastname'=>'Smith','email'=> 'jane@domain.com');

    $this->db->where('id','3');

    $this->db->update('users',$data);

    }

    }


    class Users extends Controller{

    function Users(){

    // load controller parent

    parent::Controller();

    // load database class and connect to MySQL

    $this->load->database();

    }

    // delete row from 'users' MySQL table

    function index(){

    $this->db->where('id',2);

    $this->db->delete('users');

    }

    }

    Undoubtedly, after studying the signature of the above controllers, you’ll have to agree with me that updating and deleting database rows with Code Igniter is an extremely simple process! In the first case, the controller uses the “update()” method to update a specific row in the sample MySQL table, while the second class simply deletes an existing record.

    And with this final example, I’m finishing this brief introduction to inserting, updating and deleting database rows with Code Igniter. Don’t forget to consult its user manual for a more detailed explanation of each of the methods covered in this tutorial.

    Final thoughts

    In this seventh part of the series, I provided you with a bunch of examples aimed at demonstrating how to perform insertions, updates and deletions on a selected MySQL table by using Code Igniter’s database class. As you saw before, all of these tasks are very intuitive and easy to grasp too.

    In the forthcoming chapter, I’ll be taking a look at the email class of this handy PHP framework, so you can learn how to use this class within the context of the MVC pattern. Don’t miss the next part!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT