PHP
  Home arrow PHP arrow Page 3 - Introduction to Using SQLite with PHP 5
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? 
Google.com  
PHP

Introduction to Using SQLite with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 17
    2006-12-04


    Table of Contents:
  • Introduction to Using SQLite with PHP 5
  • The basics of SQLite
  • Retrieving rows from a database table
  • Examining a few more row-fetching methods

  • 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


    Introduction to Using SQLite with PHP 5 - Retrieving rows from a database table
    ( Page 3 of 4 )

    Undoubtedly, one of the most common tasks performed when using a RDBMS is fetching rows from one or many database tables. SQLite has some helpful methods that will let you do this with only minor hassles. However, when it comes to retrieving records from a specific database table, the “fetch()” method is by far the one most used, thanks to its extreme simplicity and functionality.

    Now, with reference to the above mentioned method, below I coded a pair of examples that show how to use it, in conjunction with its associated constants. Thus, have a look at the following pair of code listings:

    // example using 'fetch()' method and SQLITE_ASSOC constant

    // create new database using the OOP approach

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

    // create table 'USERS' and insert sample data

    $db->query("BEGIN;

            CREATE TABLE users (id INTEGER(4) UNSIGNED PRIMARY KEY,
    name CHAR(255), email CHAR(255));

            INSERT INTO users (id,name,email) VALUES
    (NULL,'User1','user1@domain.com');

            INSERT INTO users (id,name,email) VALUES
    (NULL,'User2','user2@domain.com');

            INSERT INTO users (id,name,email) VALUES
    (NULL,'User3','user3@domain.com');

            COMMIT;");

    // fetch rows from the 'USERS' database table

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

    // loop over rows of database table

    while($row=$result->fetch(SQLITE_ASSOC)){

        // fetch current row

        echo $row['id'].' '.$row['name'].' '.$row['email'].'<br />';

    }

    /*

    //displays the following:

    1 User1 user1@domain.com

    2 User2 user2@domain.com

    3 User3 user3@domain.com

    */

    // example using 'fetch()' method and SQLITE_NUM constant

    // create new database using the OOP approach

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

    // create table 'USERS' and insert sample data

    $db->query("BEGIN;

            CREATE TABLE users (id INTEGER(4) UNSIGNED PRIMARY KEY,
    name CHAR(255), email CHAR(255));

            INSERT INTO users (id,name,email) VALUES
    (NULL,'User1','user1@domain.com');

            INSERT INTO users (id,name,email) VALUES
    (NULL,'User2','user2@domain.com');

            INSERT INTO users (id,name,email) VALUES
    (NULL,'User3','user3@domain.com');

            COMMIT;");

    // fetch rows from the 'USERS' database table

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

    // loop over rows of database table

    while($row=$result->fetch(SQLITE_NUM)){

        // fetch current row

        echo $row[0].' '.$row[1].' '.$row[2].'<br />';

    }

    /*

    //displays the following:

    1 User1 user1@domain.com

    2 User2 user2@domain.com

    3 User3 user3@domain.com

    */

    As you can see, the first example shows in an accessible way how the previous “fetch()” method can be used along with its “SQLITE_ASSOC” constant to retrieve rows from a specified database table as an associative array. This feature is closely similar to the “mysql_fetch_row()” PHP built-in function that you’ve used probably hundreds of times.

    Concerning the second example, the corresponding “fetch()” method is utilized this time in conjunction with the “SQLITE_NUM” constant. This comes in very handy for obtaining database rows by using only their numeric keys. 

    Of course, I’m sure that the couple of examples you learned before are pretty familiar to you, since they are very similar to the respective PHP functions used for fetching rows from MySQL. You'll hear that refrain a lot in reference to all the methods that I’ll be reviewing during the course of this series.

    Okay, now that you hopefully grasped the logic that stands behind the “fetch()” method, let’s take an in-depth look at other useful methods. In the following section I’ll show you how to use a pair of new methods to fetch all the rows from a particular result set. You'll also learn how to retrieve records by using an object-based notation.

    Wan to learn how this will be done? Please, click on the link below and keep reading.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek