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)){ ?>

<? }?>
Menu