When I run this script , I have a error :
Warning: Invalid argument supplied for foreach() in C:\Apache\test.php on line 28

<?php
$conn =OCILogon("xxxx", "yyyy", "localhost/xe");
$query = 'SELECT *FROM TEST';
$stid = OCIParse($conn, $query);

print '</br>';
$stmt=OCIParse($conn,'SELECT *FROM TEST');


$ret=OCIExecute($stmt);
$nrows=OCIFetch($stmt,$results);


if ($nrows>0)
{
echo "<table border='1' align='left' bordercolor='gray' cellspacing='0' cellpadding='5'> \n";
echo "<tr>\n";

foreach ($results as $key => $val)
{ echo "<th bgcolor='yellow' height='20' align='left'> <font color='b' face='verdana' size='2'> $key </font> </th> \n";
}
echo "</tr>\n";


for ($i=0; $i<$nrows; $i++) {
echo "<tr>\n";

foreach ($results as $data){


if($data[$i] == null)
{
   echo "<td bgcolor='white'  align='left'> <FONT COLOR='black' FACE='verdana' align='left' SIZE='3'> &nbsp </FONT> </td>\n";
}
else
if($data[$i] == 'FAIL')
{
echo "<td bgcolor='red' > <FONT COLOR='black' FACE='verdana' SIZE='2'> $data[$i] </font>  </td>  \n";
}
else

{
echo "<td bgcolor='white' > <FONT COLOR='black' FACE='verdana' SIZE='2'> $data[$i] </font>  </td>  \n";
}

}
echo "</tr>\n";
}
echo "<table> \n"; 

}else 

{
echo '<font color="red"> missing id <br/> </font>';
}


//oci_free_statement($stmt);
  OCIFreeStatement($stmt);
  OCILogoff($conn);

I modify this code and insert (array) :

foreach ((array)$results as $key => $val)
{ echo "<th bgcolor='yellow' height='20' align='left'> <font color='b' face='verdana' size='2'> $key </font> </th> \n";
}
echo "</tr>\n";


for ($i=0; $i<$nrows; $i++) {
echo "<tr>\n";

foreach ((array)$results as $data){


if($data[$i] == null)
{
   echo "<td bgcolor='white'  align='left'> <FONT COLOR='black' FACE='verdana' align='left' SIZE='3'> &nbsp </FONT> </td>\n";
}
else
if($data[$i] == 'FAIL')
{
echo "<td bgcolor='red' > <FONT COLOR='black' FACE='verdana' SIZE='2'> $data[$i] </font>  </td>  \n";
}
else

{
echo "<td bgcolor='white' > <FONT COLOR='black' FACE='verdana' SIZE='2'> $data[$i] </font>  </td>  \n";
}

}
echo "</tr>\n";
}
echo "<table> \n"; 

}else 

{
echo '<font color="red"> Dzisiaj jeszcze nie byly uruchamiane TESTY <br/> </font>';
}

Now I have a blank page , I dont have eny errors and any value on www
anyone have any idea?

Dani AI

Generated

The warning means $results was not an array. Casting with (array)$results only hides the warning — it turns null into an empty array, so the loops never run and you get a blank page. As suggested, the right approach is to actually fetch rows into an array instead of relying on the internal OCI buffers.

Two safe patterns to use with the PHP OCI API:

/* fetch all rows at once (OCIFetchStatement / oci_fetch_all) */
$stmt = OCIParse($conn, 'SELECT * FROM TEST');
OCIExecute($stmt);
$nrows = OCIFetchStatement($stmt, $results);
if ($nrows > 0) {
  for ($i = 0; $i < $nrows; $i++) {
    foreach ($results as $col => $values) {
      echo $values[$i];   // $results['COLNAME'][rowIndex]
    }
  }
}
/* fetch row-by-row (OCIFetchInto) - better for large result sets */
$stmt = OCIParse($conn, 'SELECT * FROM TEST');
OCIExecute($stmt);
while (OCIFetchInto($stmt, $row, OCI_ASSOC + OCI_RETURN_NULLS)) {
  foreach ($row as $col => $val) {
    echo $val;   // $row is one associative row
  }
}

Troubleshooting tips:

  • After a fetch, print_r($results) or var_dump($row) to confirm the array shape.
  • If a fetch function returns false/0, call oci_error($stmt) or oci_error($conn) to get details.
  • OCIFetchStatement/oci_fetch_all returns the number of rows fetched and loads everything into memory — use OCIFetchInto for streaming large sets.
  • Function names are case-insensitive, so OCIParse and oci_parse behave the same.

This explains why your cast produced nothing and shows two reliable ways to populate arrays suitable for foreach.

Recommended Answers

All 2 Replies

The problem is that $results is not an array. With ocifetch() you get the next row from a query into internal buffers so, you get a single part of the result set, try ocifetchinto(), you can read an example here:

Or try to use ocifetchstatement() which is the alias of the php5 function oci_fetch_all() and:

Fetches multiple rows from a query into a two-dimensional array. By default, all rows are returned.

A note: in order to count the rows use ocirowcount(): http://www.php.net/manual/en/function.ocirowcount.php

bye!

ocifetchstatement

it works !
thanx cereal ! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.