PHP
  Home arrow PHP arrow Page 3 - Using Unbuffered Queries and More with...
Dev Shed Forums 
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

Using Unbuffered Queries and More with SQLite with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2006-12-11

    Table of Contents:
  • Using Unbuffered Queries and More with SQLite with PHP 5
  • Working with unbuffered queries
  • Counting rows and fields of database tables
  • Analyzing more row-processing methods

  • 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


    Using Unbuffered Queries and More with SQLite with PHP 5 - Counting rows and fields of database tables


    (Page 3 of 4 )

    Indeed, one of the most common tasks performed when working with a RDBMS is counting the number of rows returned by a concrete query. Particularly, SQLite makes this process really painless. It offers a new method called “numRows().” As the name suggests, it comes in handy for determining how many rows were returned by a result set.

    Concerning the implementation of this method, below I coded an example that demonstrates how to use it:

    // example using the 'num_rows()' method

    // create new database using the OOP approximation

    $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 />';

    }

    echo 'Number of rows returned by the query: '. $result->numRows
    ();

    /*

    // displays the following:

    1 User1 user1@domain.com

    2 User2 user2@domain.com

    3 User3 user3@domain.com

    Number of rows returned by the query: 3

    */

    As illustrated above, the respective “numRows()” method is called up after a proper result set object has been created, to calculate the number of rows returned by a SELECT statement. Also, you should notice the high level of intuitiveness exposed by this method, since it allows you to perform the row-counting operation as you’d normally do with a custom class. Definitely, here is where SQLite starts shining!

    All right, now that you know how to count returned rows with the “numRows()” method that you saw before, let me show you another method. It will allow you to determine the number of fields contained in a given result set. Not surprisingly, this has been called “numFields()” and it can be used as follows:

    // example using the 'numFields()' method

    // create new database using the OOP approximation

    $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)){

        // display row

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

    }

    echo 'Number of fields in result set: '. $result->numFields();

    /*

    // displays the following

    1 User1 user1@domain.com

    2 User2 user2@domain.com

    3 User3 user3@domain.com

    Number of fields in result set: 3

    */

    In this case, the above “numFields()” method is quite useful for determining how many fields were returned in a particular result set. As you’ll realize, this method is very similar to the previous “numRows()” method, since it’s also been attached to the $result data set object. Undoubtedly, after studying the two previous examples, you’ll agree with me that SQLite makes counting table rows and fields a no-brainer process!

    So far, I demonstrated the implementation of some additional methods included with SQLite, which certainly will make your life much easier, particularly if the application you’re developing involves fairly intensive processing of result sets.

    However, I’ve not come to the end of this tutorial yet. I’d like to show you a couple of extra methods which will add some handy capabilities to your PHP applications. These methods will help when it comes to determining specific information about fetched database rows.

    Want to learn how to use these helpful methods? Click on the link below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · In this second article of the series, some methods that come packaged with SQLite...
       · Thanks again Alejandro, one of the best features heralded by the arrival of PHP5,...
       · Hello again Jon,Thank you for your kind comments on my article about SQLite, as...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - 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
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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