05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

string(4) "1962"<br />

["name"]=><br />

string(12) "Sean Connery"<br />

}<br />

The DB_FETCHMODE_OBJECT mode turns the row into an object, with a property for each<br />

column in the result row:<br />

$row = $result->fetchRow(DB_FETCHMODE_ASSOC);<br />

if (DB::isError($row)) {<br />

die($row->getMessage( ));<br />

}<br />

var_dump($row);<br />

object(stdClass)(3) {<br />

["title"]=><br />

string(5) "Dr No"<br />

["year"]=><br />

string(4) "1962"<br />

["name"]=><br />

string(12) "Sean Connery"<br />

}<br />

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

echo "{$row->title} was made in {$row->year}";<br />

Dr No was made in 1962<br />

Finishing the result<br />

A query result object typically holds all the rows returned by the query. This may<br />

consume a lot of memory. To return the memory consumed by the result of a query<br />

to the operating system, use the free( ) method:<br />

$result->free( );<br />

This is not strictly necessary, as free( ) is automatically called on all queries when<br />

the <strong>PHP</strong> script ends.<br />

Disconnecting<br />

To force <strong>PHP</strong> to disconnect from the database, use the disconnect( ) method on the<br />

database object:<br />

$db->disconnect( );<br />

This is not strictly necessary, however, as all database connections are disconnected<br />

when the <strong>PHP</strong> script ends.<br />

Advanced Database Techniques<br />

PEAR DB goes beyond the database primitives shown earlier; it provides several<br />

shortcut functions for fetching result rows, as well as a unique row ID system and<br />

separate prepare/execute steps that can improve the performance of repeated queries.<br />

Advanced Database Techniques | 197<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!