Two while cannot be applied to the same query?

encountered a problem today

$sql = $mysqli->query(
              "SELECT .... "
            );

I have a query that I want to apply to two while, but only the first one has a response

<? while ($row = mysqli_fetch_array($sql)){ ?>

<? }?>

<? while ($row = mysqli_fetch_array($sql)){ ?>

<? }?>

is there any way to implement the same query that can be applied to two while? In the same place.

Apr.07,2021

you can save the results of fetch to an array and then cycle through the array.

$rows=array();
while($row=mysqli_fetch_array($result)) {
$rows[]=$row;
}

foreach($rows as $row) {
A
}

foreach($rows as $row) {
B
}

or simply use mysqli_fetch_all , which is said to have some performance advantages:

$rows=mysqli_fetch_all($result, MYSQLI_BOTH);

Note mysqli_fetch_all defaults to MYSQLI_NUM , which is different from mysqli_fetch_array , so add a second parameter.


add a sentence mysql_data_seek ($sql, 0)
before the second while. Excuse me if there is anything wrong with the grammar of php,.

<? while ($row = mysqli_fetch_array($sql)){ ?>

<? }?>

<? mysql_data_seek( $sql, 0 ); ?>

<? while ($row = mysqli_fetch_array($sql)){ ?>

<? }?>
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3689a-2c04d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3689a-2c04d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?