Jump to content

function for while loop expression


Recommended Posts

I have a function that contains a while loop.  I would like to have the function pass data back to another while loop.  The best way to demonstrate this would be with the following example:

 

while ($row = oci_fetch_array($stid, OCI_BOTH)) {
    // Use the uppercase column names for the associative array indices
    echo $row[0] . " and " . $row['DEPARTMENT_ID']   . " are the same<br>\n";
    echo $row[1] . " and " . $row['DEPARTMENT_NAME'] . " are the same<br>\n";
}

 

Except in my case, oci_fetch_array, would be a function that I create.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/208923-function-for-while-loop-expression/
Share on other sites

I have a wrapper function that performs a select:

 

// select function
function selxl($dbh, $sql, $bind) {
  // parse the sql statement
  $sth = oci_parse($dbh, $sql) or die(oci_error());

  // bind
  foreach ($bind as $key => $val) {
    // is the bind variable in the sql
    if (strpos($sql, $key)) {
      oci_bind_by_name($sth, $key, $bind[$key]) or die(oci_error());
    }
  }

  // execute the statement
  oci_execute($sth) or die(oci_error());

  // fetch records
  while ($row = oci_fetch_array($sth, OCI_NUM + OCI_RETURN_NULLS + OCI_RETURN_LOBS)) {
    // return
    return $row;
  }

  // cleanup resources associated sql
  oci_free_statement($sth);
}

 

I have a while loop that this function is called from:

 

while ($data = selxl($dbh, $sql, $bind)) {
  // deal with $data
}

 

The problem that I am having is that "return $row;" in my function breaks the loop in that function.

 

I need to know how to return multiple times from the function.  Something that works like the oci_fetch_array function (I am guessing that this is written in C and C has this functionality).  I can't seem to find an example of how to do this anywhere.  Is it even possible with PHP?

The way you have constructed your function you'll probably want to return an array:

 

  // fetch records
  while ($row = oci_fetch_array($sth, OCI_NUM + OCI_RETURN_NULLS + OCI_RETURN_LOBS)) {
      $allrows[] = $row;
  }
  // cleanup resources associated sql
  oci_free_statement($sth);

  // return
  return $allrows;

 

Then loop over the resultant array:

 

foreach (selxl($dbh, $sql, $bind) as $data) {
  // deal with $data
}

The way you have constructed your function you'll probably want to return an array:

 

Thanks for the reply.

 

I have a function like that, but I can't use it because my result set exceeds the memory that PHP is configured for.  I have to loop through each row individually.

Really the only other way to do it is to return your statement identifier, though I have never used Oracle:

 

// select function
function selxl($dbh, $sql, $bind) {
  // parse the sql statement
  $sth = oci_parse($dbh, $sql) or die(oci_error());

  // bind
  foreach ($bind as $key => $val) {
    // is the bind variable in the sql
    if (strpos($sql, $key)) {
      oci_bind_by_name($sth, $key, $bind[$key]) or die(oci_error());
    }
  }

  // execute the statement
  oci_execute($sth) or die(oci_error());

  return $sth;
}

 

And then:

 

$sth = selxl($dbh, $sql, $bind);

while ($data = oci_fetch_array($sth, OCI_NUM + OCI_RETURN_NULLS + OCI_RETURN_LOBS)) {
   // deal with $data
}
// cleanup resources associated sql
oci_free_statement($sth);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.