PHP
  Home arrow PHP arrow Page 4 - Additional Methods for Using SQLite wi...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Additional Methods for Using SQLite with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2006-12-18

    Table of Contents:
  • Additional Methods for Using SQLite with PHP 5
  • Using the seek() and lastInsertRowid() methods
  • Using the changes() and queryExec() methods
  • Using the createFunction() method
  • Creating databases in server memory

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Additional Methods for Using SQLite with PHP 5 - Using the createFunction() method


    (Page 4 of 5 )

    As I explained in the previous section, the SQL library has been provided with the ability to work with iterators to traverse a specific result set by using only a typical “foreach” language construct. Certainly, you’ll have to agree with me that this feature is really handy, since there’s no need to write custom code for iterating over data sets.

    To learn more about the use of iterators with SQLite, please have a look at the following example, which shows a simple implementation of this neat concept:

    // example of 'SQLite' iterators

    // create new database using the OOP approximation

    $db=new SQLiteDatabase("db.sqlite");

    // fetch rows from the 'USERS' database table

    $result=$db->unbufferedQuery("SELECT * FROM users");

    // use 'foreach' loop to traverse result set

    foreach($result as $row){

    echo 'ID: '.$row['id'].' Name :'.$row['name'].' Email :'.$row
    ['email'].'<br />';

    }

    /* displays the following:

    ID: 1 Name :User1 Email :user1@domain.com

    ID: 2 Name :User2 Email :user2@domain.com

    ID: 3 Name :User3 Email :user3@domain.com

    */

    As you can see, the above code snippet demonstrates how a specified result set can be traversed by using a simple iterator. In this case, the script first obtains the mentioned data set via the “unbufferedQuery()” method you learned before and finally uses a common “foreach” loop to traverse the data structure in question. Simple and efficient, isn’t it?

    Now that you hopefully grasped the concept that stands behind using iterators with SQLite, it’s time to look at another useful method which I’m certain you’ll find very handy. In this case I’m talking about the “createFunction()” method. As the name clearly suggests, it's really helpful for creating user-defined functions that can be tied to a particular result set or as part of a WHERE clause.

    With reference to this excellent capability, below I coded a basic example of how to use a custom function with SQLite. Take a look a the corresponding code sample:

    // example of custom functions

    // create custom function

    function getRandomID($id){

                return rand($id,5);

    }

    // create new database using the OOP approximation

    $db=new SQLiteDatabase("db.sqlite");

    $db->createFunction('getRandomID','getRandomID',1);

    // fetch rows from the 'USERS' database table

    $result=$db->query("SELECT * FROM users WHERE id==getRandomId(2)");

    foreach($result as $row){

                echo 'ID: '.$row['id'].' Name :'.$row['name'].'
    Email :'.$row['email'].'<br />';

    }

    /*

    displays the following

    ID: 2 Name :User2 Email :user2@domain.com

    */

    As you’ll realize, the above example begins creating the custom “getRandom()” function, which obviously returns a random integer between 1 and 5. After this function has been created, it’s used as part of the corresponding  SELECT statement to fetch a random row from the respective database table.

    Of course, this is only a basic application of using custom functions with SQLite, which means that you can experiment by defining your own, certainly more useful functions.

    So far, I covered the most important methods that come bundled with the SQLite library. But I must say I’m not finished yet, since I’d like to teach you another cool feature included with this tight RDBMS.

    Remember that in the beginning of this series I mentioned the capability offered by SQLite for working with memory-based databases? I hope you do, because in the last section of this article, I’ll show you how to implement this characteristic in your own database-driven PHP applications.

    Therefore, be a bit more patient and read the last lines of this tutorial.

    More PHP Articles
    More By Alejandro Gervasio


       · Over the course of this final installment of the series, you'll see how to perform...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT