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.

Storing the row<br />

The fetchInto( ) method also gets the next row, but stores it into the array variable<br />

passed as a parameter:<br />

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

Like fetchRow( ), fetchInto( ) returns NULL if there is no more data, or DB_ERROR if an<br />

error occurs.<br />

The idiom to process all results looks like this with fetchInto( ):<br />

while ($success = $result->fetchInto($row)) {<br />

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

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

}<br />

// do something with the row<br />

}<br />

Inside a row array<br />

Just what are these rows that are being returned? By default, they’re indexed arrays,<br />

where the positions in the array correspond to the order of the columns in the<br />

returned result. For example:<br />

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

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

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

}<br />

var_dump($row);<br />

array(3) {<br />

[0]=><br />

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

[1]=><br />

string(4) "1962"<br />

[2]=><br />

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

}<br />

You can pass a mode parameter to fetchRow( ) or fetchInto( ) to control the format of<br />

the row array. The default behavior, shown previously, is specified with DB_<br />

FETCHMODE_ORDERED.<br />

The fetch mode DB_FETCHMODE_ASSOC creates an array whose keys are the column<br />

names and whose values are the values from those columns:<br />

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

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

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

}<br />

var_dump($row);<br />

array(3) {<br />

["title"]=><br />

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

["year"]=><br />

196 | Chapter 8: Databases<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!