Home arrow PHP arrow Page 2 - Database Techniques and PHP

Issuing a Query - PHP

Picking up where we left off last week in our discussion of databases and PHP, we'll talk about connecting, issuing a query, and more. This article is excerpted from chapter eight of the book Programming PHP, Second Edition, written by Kevin Tatroe, Rasmus Lerdorf, and Peter MacIntyre (O'Reilly, 2006; ISBN: 0596006810). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

TABLE OF CONTENTS:
  1. Database Techniques and PHP
  2. Issuing a Query
  3. Inside a row array
  4. Advanced Database Techniques
By: O'Reilly Media
Rating: starstarstarstarstar / 4
June 21, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

The query() method on a database object sends SQL to the database:

  $result = $db->query(sql);

A SQL statement that doesn’t query the database (e.g.,INSERT,UPDATE,DELETE) returns theDB_OKconstant to indicate success. SQL that performs a query (e.g.,SELECT) returns an object that you can use to access the results.

You can check for success withDB::isError():

  $q = $db->query($sql);
  if (DB::iserror($q)) {
    die($q->getMessage());
  }

Fetching Results from a Query

PEAR DB provides two methods for fetching data from a query result object. One returns an array corresponding to the next row, and the other stores the row array into a variable passed as a parameter.

Returning the row

The fetchRow() method on a query result returns an array of the next row of results:

  $row = $result->fetchRow([ mode ]);

This returns either an array of data, NULLif there is no more data (or none to begin with—an empty result), orDB_ERRORif an error occurred. Themode  parameter controls the format of the array returned, which is discussed later.

This common idiom uses thefetchRow()method to process a result, one row at a time, as follows:

  while ($row = $result->fetchRow()){
    if (DB::isError($row)) {
     
die($row->getMessage());
    }
    // do something with the row
  }

Storing the row

The fetchInto() method not only gets the next row, but also stores it into the array variable passed as a parameter:

  $success = $result->fetchInto(array, [mode]);

LikefetchRow(),fetchInto()returnsNULLif there is no more data, orDB_ERRORif an error occurs.

The idiom to process all results looks like this withfetchInto():

  while ($success = $result->fetchInto($row)) {
    if (DB::isError($success)) {
     
die($success->getMessage());
    }
    // do something with the row
  }



 
 
>>> More PHP Articles          >>> More By O'Reilly Media
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: