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"); } ?> Link to comment https://forums.phpfreaks.com/topic/89560-problem-executing-successive-db2_fetch_array-command/ 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? Link to comment https://forums.phpfreaks.com/topic/89560-problem-executing-successive-db2_fetch_array-command/#findComment-458791 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. Link to comment https://forums.phpfreaks.com/topic/89560-problem-executing-successive-db2_fetch_array-command/#findComment-458798 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 Link to comment https://forums.phpfreaks.com/topic/89560-problem-executing-successive-db2_fetch_array-command/#findComment-458802 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.