I have a while loop that contains another while loop. Both loops are iterating over different mysql result sets. The problem is that the second time the outer while loop calls the inner while loop it doesn't run. Does Mysql_fetch_row discard itself after it has been iterated over?
Here is what the code looks like:
$counter = 0;
while ($r = mysql_fetch_row($result)){
$col = "";
$val = "";
while($d = mysql_fetch_row($dictionary)){
$key = $d[0];
for($i= 0; $i< count($tableColumNames); $i++){
if($tableColumNames[$i] == $key){
$col .= "`".$d[1]."` ,";
$val .= "`".$r[$i]."` ,";
/* echo $tableColumNames[$i]." table ColumnName <br>";
echo $d[1]." key<br>"; */
}
}
echo $key."round".$counter."<br>";
}
var_dump ($col);
var_dump ($val);
$counter++;
echo $counter;
}
And here is what the output is like: You can see that the $result holds four records and the output is showing that the loop is working correctly. However, the inner loop over the $dictionary result set doesn't run the second time $result is being iterated over. Any ideas why? I tried to use mysql_fetch_array as well but still the same result.
Thanks
$dictionary
result resource? Each time you call one of themysql_fetch*()
functions, the result resource's internal pointer is advanced. If you intend to use the same row over and over, you would need to be rewinding the resource withmysql_data_seek()
, but better would be to store all its rows in an array first and use that to loop over (rather than doing your main logic while looping the result resource) – Michael Berkowski Jun 8 at 1:39mysql_*()
functions are deprecated and shouldn't be used for new code. Consider switching to MySQLi or PDO if possible -> Why shouldn't I usemysql_*
functions – Michael Berkowski Jun 8 at 1:43