简体   繁体   中英

MySQL while loop only loops once

I have a database activities that stores activities information such as attendees, event description, event time, etc.

I call a while statement to print out the data, however the while loops is only looping once:

$result = mysqli_query($conn, "SELECT * FROM `activities` ORDER BY `date`");
while($act = mysqli_fetch_array($result)){ 
   //do some stuff
}

I did a var_dump of $result , and this is printed out:

object(mysqli_result)#3 (5) { ["current_field"]=> int(0) ["field_count"]=> int(11) ["lengths"]=> NULL ["num_rows"]=> int(26) ["type"]=> int(0) }

I have no idea what I'm doing wrong, can someone help? thanks!

As per comments on the question. Overwriting $result was the cause of this problem.

This is supposed to work.

$result = mysqli_query($conn, "SELECT * FROM `activities` ORDER BY `activities`.`date`");

while($act = mysqli_fetch_array($result)){ 
   //do some stuff
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM