mongo23 Posted February 5, 2008 Share Posted February 5, 2008 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"); } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted February 5, 2008 Share Posted February 5, 2008 This... $fields[$ix] > " " is probably going to give you a headache for starters. What is within the $fields array? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 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. Quote Link to comment Share on other sites More sharing options...
craygo Posted February 5, 2008 Share Posted February 5, 2008 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.