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?
try it.....
Yes.
mysqli_fetch_array()
moves the pointer forward each time you call it. You needmysqli_data_seek()
to set the pointer back to the start and then callmysqli_fetch_array()
again.So before calling the function a second time, do:
You should always separate data manipulations from output.
Select your data first:
Note that in the recent PHP versions you can use
fetch_all()
instead of the explicit loop:Then use it as many times as you wish: