Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Oracle Functions : oci_fetch_array

oci_fetch_array

Returns the next row from the result data as an associative or numeric array, or both (PHP 5, PECL oci8:1.1-1.2.4)
array oci_fetch_array ( resource statement [, int mode] )

Returns an array, which corresponds to the next result row.

For details on the data type mapping performed by the oci8 driver, see the datatypes supported by the driver

It should be mentioned here, that oci_fetch_array() is insignificantly slower, than oci_fetch_row(), but much more handy.

Parameters

statement

A valid OCI statement identifier.

statement

An optional second parameter can be any combination of the following constants:

OCI_BOTH - return an array with both associative and numeric indices (the same as OCI_ASSOC + OCI_NUM). This is the default behavior.
OCI_ASSOC - return an associative array (as oci_fetch_assoc() works).
OCI_NUM - return a numeric array, (as oci_fetch_row() works).
OCI_RETURN_NULLS - create empty elements for the NULL fields.
OCI_RETURN_LOBS - return the value of a LOB of the descriptor.

Default mode is OCI_BOTH.

Return Values

Returns an array with both associative and numeric indices, or FALSE if there are no more rows in the statement.

Note:

This function sets NULL fields to PHP NULL value.

Note:

Oracle returns all field names in uppercase and associative indices in the result array will be uppercased too.

Examples

Example1643.oci_fetch_array() with OCI_BOTH example

<?php
$connection
= oci_connect("apelsin", "kanistra");

$query = "SELECT id, name FROM fruits";

$statement = oci_parse ($connection, $query);
oci_execute ($statement);

while (
$row = oci_fetch_array ($statement, OCI_BOTH)) {
echo
$row[0]." and ".$row['ID']." is the same<br>";
echo
$row[1]." and ".$row['NAME']." is the same<br>";
}
?>


Example1644.oci_fetch_array() with OCI_NUM example

<?php
$connection
= oci_connect("user", "password");

$query = "SELECT id, name, lob_field FROM fruits";

$statement = oci_parse ($connection, $query);
oci_execute ($statement);

while (
$row = oci_fetch_array ($statement, OCI_NUM)) {
echo
$row[0]."<br>";
echo
$row[1]."<br>";
echo
$row[2]->read(100)."<br>"; //this will output first 100 bytes from LOB
}
?>


Example1645.oci_fetch_array() with OCI_ASSOC example

<?php
$connection
= oci_connect("user", "password");

$query = "SELECT id, name, lob_field FROM fruits";

$statement = oci_parse ($connection, $query);
oci_execute ($statement);

while (
$row = oci_fetch_array ($statement, OCI_ASSOC)) {
echo
$row['ID']."<br>";
echo
$row['NAME']."<br>";
echo
$row['LOB_FIELD']."<br>"; //this will output "Object id #1"
}
?>


Example1646.oci_fetch_array() with OCI_RETURN_LOBS example

<?php
$connection
= oci_connect("user", "password");

$query = "SELECT id, name, lob_field FROM fruits";

$statement = oci_parse ($connection, $query);
oci_execute ($statement);

while (
$row = oci_fetch_array ($statement, (OCI_NUM+OCI_RETURN_LOBS))) {
echo
$row[0]."<br>";
echo
$row[1]."<br>";
echo
$row['LOB_FIELD']."<br>"; //this will output LOB's content
}
?>


Examples ( Source code ) oci_fetch_array

<?
$c 
oci_connect("hr""hrpwd""//localhost/XE");
$s oci_parse($c"select region_name,
           regions.region_id as myreg,
           country_name,
           countries.region_id
            from countries
            inner join regions
            on countries.region_id = regions.region_id"
);
     
oci_execute($s);
while (
$res oci_fetch_array($s)) {
      echo 
$res["REGION_NAME"] . " " $res["MYREG"] . " - " .
      
$res["COUNTRY_NAME"] . " " $res["REGION_ID"] . " " .
      
"<br>\n";
}
?>
The query column alias MYREG is used as the index to the result array. The script output is:
Americas 2 - Argentina 2
Asia 3 - Australia 3
Europe 1 - Belgium 1
Americas 2 - Brazil 2
Americas 2 - Canada 2


oci_fetch_array() with OCI_NUM example
<?php
$connection 
oci_connect("user""password");

$query "SELECT id, name, lob_field FROM fruits";

$statement oci_parse ($connection$query);
oci_execute ($statement);

while (
$row oci_fetch_array ($statementOCI_NUM)) {
    echo 
$row[0]."<br>";
    echo 
$row[1]."<br>";
    echo 
$row[2]->read(100)."<br>";  //this will output first 100 bytes from LOB
}
?>



oci_fetch_array() with OCI_RETURN_LOBS example
<?php
$connection 
oci_connect("user""password");

$query "SELECT id, name, lob_field FROM fruits";

$statement oci_parse ($connection$query);
oci_execute ($statement);

while (
$row oci_fetch_array ($statement, (OCI_NUM+OCI_RETURN_LOBS))) {
    echo 
$row[0]."<br>";
    echo 
$row[1]."<br>";
    echo 
$row['LOB_FIELD']."<br>";  //this will output LOB's content
}
?>

Related Examples ( Source code ) » oci_fetch_array




Code Examples / Notes » oci_fetch_array

robert dot hicks

OCI_NUM should be changed in the various scripts to the appropriate call to the OCI_*. For example the script to show the OCI_ASSOC call still has OCI_NUMS in it.

09-jun-2005 04:13

OCI_BOTH is the default.
If you just need to return all fields, you can leave out OCI_NUM & OCI_ASSOC.
EXAMPLE:
<?php
$connection = ocilogon("user", "password", "dbname");
$query = "SELECT id, name, lob_field FROM fruits";
$statement = oci_parse ($connection, $query);
oci_execute ($statement);
while ($row = oci_fetch_array ($statement)) {
  $id = $row['ID'];
  $name = $row['NAME'];
  $lob_field = $row['LOB_FIELD'];
  echo $id.' '.$name.' '.$lob_field;
}
?>


stry_cat

If you want to get both nulls and an assoc array, you have to ADD the values like this:
$row = oci_fetch_array($stmt, OCI_RETURN_NULLS + OCI_ASSOC);
This really should be noted in the text of the manual.


badr

generating dynamic drop down list
<SELECT name="reason" id="vacation_code">
<OPTION value=0 selected>Choose </OPTION>
<?php
$query2 = "SELECT IN_NAME, IN_CODE FROM HR_IN_REASON ";
$statement2 = oci_parse ($conn, $query2);
oci_execute ($statement2);
while ($row = oci_fetch_array ($statement2, OCI_NUM)) {
?>
                 <option value="<?  echo $row[1]; ?>"> <? echo $row[0] ?> </option>
                 <? }
?>
               </select>


dwhitaker

Example 3 above is incorrect...
OCI_NUM should be OCI_ASSOC
So it should read something like this:
<?php
$connection = ocilogon("user", "password", "dbname");
$query = "SELECT id, name, lob_field FROM fruits";
$statement = oci_parse ($connection, $query);
oci_execute ($statement);
while ($row = oci_fetch_array ($statement, OCI_ASSOC)) {
  $id = $row['ID'];
  $name = $row['NAME'];
  $lob_field = $row['LOB_FIELD'];
  echo $id.' '.$name.' '.$lob_field;
}
?>


antonchanning

As Robert Hicks mentioned back in August 2004 there is an error in examples 3 and 4 of this page.  The error in example 3 is what dwhitaker and stry_cat address in their notes of 20 May 2005 and 9 June 2005 respectively.
The correct form of example 4 should read:
<?php
$connection = oci_connect("user", "password");
$query = "SELECT id, name, lob_field FROM fruits";
$statement = oci_parse ($connection, $query);
oci_execute ($statement);
while ($row = oci_fetch_array ($statement, OCI_RETURN_LOBS)) {
  echo $row[0]."
";
  echo $row[1]."
";
  echo $row['LOB_FIELD']."
";  //this will output LOB's content
}
?>
This really should be corrected in the actual documentation...


Change Language


Follow Navioo On Twitter
oci_bind_array_by_name
oci_bind_by_name
oci_cancel
oci_close
OCI-Collection->append
OCI-Collection->assign
OCI-Collection->assignElem
OCI-Collection->free
OCI-Collection->getElem
OCI-Collection->max
OCI-Collection->size
OCI-Collection->trim
oci_commit
oci_connect
oci_define_by_name
oci_error
oci_execute
oci_fetch_all
oci_fetch_array
oci_fetch_assoc
oci_fetch_object
oci_fetch_row
oci_fetch
oci_field_is_null
oci_field_name
oci_field_precision
oci_field_scale
oci_field_size
oci_field_type_raw
oci_field_type
oci_free_statement
oci_internal_debug
OCI-Lob->append
OCI-Lob->close
oci_lob_copy
OCI-Lob->eof
OCI-Lob->erase
OCI-Lob->export
OCI-Lob->flush
OCI-Lob->free
OCI-Lob->getBuffering
OCI-Lob->import
oci_lob_is_equal
OCI-Lob->load
OCI-Lob->read
OCI-Lob->rewind
OCI-Lob->save
OCI-Lob->saveFile
OCI-Lob->seek
OCI-Lob->setBuffering
OCI-Lob->size
OCI-Lob->tell
OCI-Lob->truncate
OCI-Lob->write
OCI-Lob->writeTemporary
OCI-Lob->writeToFile
oci_new_collection
oci_new_connect
oci_new_cursor
oci_new_descriptor
oci_num_fields
oci_num_rows
oci_parse
oci_password_change
oci_pconnect
oci_result
oci_rollback
oci_server_version
oci_set_prefetch
oci_statement_type
ocibindbyname
ocicancel
ocicloselob
ocicollappend
ocicollassign
ocicollassignelem
ocicollgetelem
ocicollmax
ocicollsize
ocicolltrim
ocicolumnisnull
ocicolumnname
ocicolumnprecision
ocicolumnscale
ocicolumnsize
ocicolumntype
ocicolumntyperaw
ocicommit
ocidefinebyname
ocierror
ociexecute
ocifetch
ocifetchinto
ocifetchstatement
ocifreecollection
ocifreecursor
ocifreedesc
ocifreestatement
ociinternaldebug
ociloadlob
ocilogoff
ocilogon
ocinewcollection
ocinewcursor
ocinewdescriptor
ocinlogon
ocinumcols
ociparse
ociplogon
ociresult
ocirollback
ocirowcount
ocisavelob
ocisavelobfile
ociserverversion
ocisetprefetch
ocistatementtype
ociwritelobtofile
ociwritetemporarylob
eXTReMe Tracker