Jump to content

Problem executing successive db2_fetch_array command


mongo23

Recommended Posts

I have an array that contains x number of values. I loop through the array and, for each occurance, do a select from a table. The problem is that only the first fetch works. Every successive try does not return a row. It's running on an AS400 so the database commands start with DB2 rather than 'mysql' but I believe they work the same way. I'm wondering if I have to somehow free up or clear the fetch before another can be done. Thanks in advance for your help.

 

<?

for ($ix=0; $fields[$ix] > " "; $ix++) {

$selfield = $fields[$ix];

$sql = "select * from qgpl.phpgip where name = " . " '$selfield' ";

$stmt = db2_prepare($conn, $sql);

$result = db2_execute($stmt);

 

while ($row = db2_fetch_array($stmt)) {

$fname[$ix] = $row[21];

$dtype[$ix] = $row[4]; }

}

 

for ($ix=0; $fields[$ix] > " "; $ix++) {

print("<td><b>$fname[$ix]</b></td>\n");

}

?>

while ($row = db2_fetch_array($stmt)) {

$fname[$ix] = $row[21];

$dtype[$ix] = $row[4]; }

 

You logic is flawed. The above while loop will keep overwriting $fname[$ix] and $dtype[$ix]. You want to do some sort of append I assume, either building an array or concatenating a string.

your for statement should always have some kind of count to go by other wise it will run infinitely.

 

if $fields is already an array you may be better off using a foreach statement.

 

also if the db functions work anything like oci then your code is a little off. Also seems like you got alot of extra code to just echo out some data

 

<?php
foreach($fields as $value){
$selfield = $value
$sql = "select * from qgpl.phpgip where name = '".$selfield."'";
$stmt = db2_prepare($conn, $sql);
$result = db2_execute($stmt);

   while ($row = db2_fetch_array($stmt)) {
   echo "<tr><td>".$row[21]."</td><td>".$row[4]."</td></tr>\n"; 
   }
}
?>

 

That will put each loop on it's own row

 

Ray

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.