I am using the entries of a db to fill a row and a column in a table. But I cannot access the SQL returned data twice using mysqli_fetch_array()
twice. This doesn't work:
//Copy the result
$db_res = mysqli_query( $db_link, $sql );
$db_res2=$db_res;
//Top row
while ($row = mysqli_fetch_array( $db_res, MYSQL_ASSOC))
{
echo "<td>". $row['Title'] . "</td>";
}
//leftmost column
while ($row = mysqli_fetch_array( $db_res2, MYSQL_ASSOC))
{
echo "<tr>";
echo "<td>". $row['Title'] . "</td>";
.....
echo "</tr>";
}
How can I apply mysqli_fetch_array
twice on the same result?
You don't need the
while
loop and you don't need to usemysqli_fetch_array()
at all.You can simply loop on the
mysqli_result
object itself many times.However, you should separate your DB logic from your display logic and to achieve this it is best to use
fetch_all(MYSQLI_ASSOC)
in your DB logic to retrieve all records into an array.