Steef
Steef

Reputation: 615

Looping array oci_fetch_all by number

I'm fetching results from Oracle using the following statement :

$nrows_g = oci_fetch_all( $geos, $r1, 0, -1, OCI_ASSOC + OCI_FETCHSTATEMENT_BY_ROW);

While I am able to enumerate the results by a foreach construct, I need to access them using a for loop or even better by columnnames. I must be missing something, but I can't get that to work. Can anybody point me in the right direction ?

thanks, Steef

Upvotes: 0

Views: 5733

Answers (1)

timclutton
timclutton

Reputation: 13024

You'll generally realise better performance if you don't use oci_fetch_all() because this will retrieve the entire resultset into memory. Fetching and processing each row iteratively keeps the memory usage drastically lower.

$conn = oci_connect($user, $pass, $server);
$stmt = oci_parse($conn, 'select field1, field2 from blah');
oci_execute($stmt);
while (false !== ($row = oci_fetch_assoc($stmt)) {
    echo 'Field 1: ' . $row['FIELD1'] . '<br>Field 2: ' . $row['FIELD2'] . '<br>';
}

If you really want to use a for loop and oci_fetch_all() you can do this:

$conn = oci_connect($user, $pass, $server);
$stmt = oci_parse($conn, 'select field1, field2 from blah');
oci_execute($stmt);
$count = oci_fetch_all($stmt, $result, 0, -1, OCI_FETCHSTATEMENT_BY_ROW);
for ($i = 0; $i < $count; $i++) {
    echo 'Field 1: ' . $result[$i]['FIELD1'] . '<br>Field 2: ' . $result[$i]['FIELD2'] . '<br>';
}

Upvotes: 1

Related Questions