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");

}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.