Home arrow PHP arrow Page 3 - Database Techniques and PHP

Inside a row array - 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

Just what are these rows that are being returned? By default, they’re indexed arrays, where the positions in the array correspond to the order of the columns in the returned result. For example: 

  $row = $result->fetchRow();
  if (DB::isError($row)) {

    die($row->getMessage());
  }
  var_dump($row);
 
array(3) {
    [0]=>
    string(5) "Foundation"
    [1]=>
    string(4) "1951"
    [2]=>
    string(12) "Isaac Asimov"
  }

You can pass a mode  parameter to fetchRow() or fetchInto() to control the format of the row array. The default behavior, shown previously, is specified with DB_FETCHMODE_ORDERED.

The fetch modeDB_FETCHMODE_ASSOCcreates an array whose keys are the column names and whose values are the values from those columns:

  $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
  if (DB::isError($row)) {
   
die($row->getMessage());
  }
  var_dump($row);
  array(3) {
    ["title"]=>
    string(5) "Foundation"
    ["pub_year"]=>
    string(4) "1951"
    ["name"]=>
    string(12) "Isaac Asimov"
 
}

The DB_FETCHMODE_OBJECT mode turns the row into an object with a property for each column in the result row:

  $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
  if (DB::isError($row)) {
   
die($row->getMessage());
  }
  var_dump($row);
 
object(stdClass)(3) {
    ["title"]=>
    string(5) "Foundation"
    ["pub_year"]=>
    string(4) "1951"
    ["name"]=>
   
string(12) "Isaac Asimov"
  }

To access data in the object, use the $object->property  notation:

  echo "{$row->title} was published in {$row->pub_year} and was written by {$row-> name}";
 
Foundation was published in 1951 and was written by Isaac Asimov

Finishing the result

A query result object typically holds all the rows returned by the query. This may consume a lot of memory. To return the memory consumed by the result of a query to the operating system, use the free() method:

  $result->free();

This is not strictly necessary, asfree()is automatically called on all queries when the PHP script ends.

Disconnecting

To force PHP to disconnect from the database, use the disconnect() method on the database object:

  $db->disconnect();

This too is not strictly necessary, however, as all database connections are disconnected when the PHP script ends.



 
 
>>> 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: