this one is really puzzling me, so thanks for any help. I am trying to output the values of a SQL result set, but the foreach loop appears to be iterating twice, then incrementing the array index. Such that if my DB row holds the contiguous integers 1,2,3,4,5 the output of the foreach loop is 1,1,2,2,3,3,4,4,5,5. Can’t see the reason. Code follows.
$result = mysql_query("SELECT * FROM Test");
echo "<table border=1><tr></tr>";
While($row = mysql_fetch_array($result))
{
echo "<tr>";
foreach($row as $value)
{
echo "<td>" . $value . "</td>";
}
echo "</tr><tr>";
for($x=0;$x<5;$x++)
{
echo "<td>" . $row[$x] . "</td>";
}
echo "</tr>";
}
The second for loop was thrown in afterwards for debugging, to make sure the array didn’t actually contain two instances of each value. But that loop outputs as expected: 1,2,3,4,5. Any Ideas? Thanks.
By default,
mysql_fetch_array‘s second parameter is set toMYSQL_FETCH_BOTH, which will register each value under the index as well as the name of the column. SupplyMYSQL_FETCH_NUMto get only numeric indices: