Calling function from while loop causing infinite loop


Calling function from while loop causing infinite loop



I have a MySQLi statement in a function as follows:


function getTimeData($time) {
$con = mysqli_connect("domain:port", "user", "password", "dbname") or die (mysql_error());
$selectedDate = $_SESSION['date'];

$prepTimeData = mysqli_prepare($con, "SELECT * FROM `time` WHERE `date`=? AND `time`<=? AND `endTime`>=?");
$prepTimeData->bind_param("sss", $selectedDate, $time, $time);
$prepTimeData->execute();
$getTimeData = $prepTimeData->get_result();

return $getTimeData;
}



and then a while loop to iterate over all the results:


while ($data0800 = getTimeData("0800")->fetch_assoc()) {
?><tr><td><?php $data0800['first_name'];?></td>
<td><?php $data0800['riderage'];?></td>
<td><?php $data0800['ridinglevel'];?></td>
<td><?php $data0800['horse'];?></tr><?php
}



It causes the table to just grow infinitely large without actually populating what it should. Having the statements outside the function works fine, however.





mysql_error() btw does not fit here.
– u_mulder
Jun 29 at 10:29


mysql_error()




1 Answer
1



You call the following code;


while ($data0800 = getTimeData("0800")->fetch_assoc()) {



Seems fair enough right? But every time the while loops, the function is called, which means a new query is done, in turn meaning it never reaches the end as the value is reset to the first row it finds, instead of calling the function each time the loop loops, using the following to separate the function call from the loop should make it work as you'd expect, collecting each row in turn;


$timedata = getTimeData("0800");
while ($data0800 = $timedata->fetch_assoc()) {



This is because you are only calling your function once and storing it's result in the $timedata variable, rather than refreshing the query and result set every time you go through the loop


$timedata





fetch_assoc() is already sorting the results in an array right? $timedata = getTimeData("0800"); $datas = $timedata->fetch_assoc() followed by foreach ($datas as $data){} is also an option to handle this
– Infiltrator
Jun 29 at 10:26



fetch_assoc()


$timedata = getTimeData("0800"); $datas = $timedata->fetch_assoc()


foreach ($datas as $data){}





fetch_assoc() collects a single row, if the OP wants to use each row in turn (as shown by the code in the loop in question), a while works better as it gets each row, one at a time, rather than a field at a time which foreach would, foreach would loop the data within the row, and not the rows themselves
– Sam Swift 웃
Jun 29 at 10:28



fetch_assoc()


while


foreach





Brilliant. That solved it. Many thanks.
– Lewis Smith
Jun 29 at 10:45





@LewisSmith - happy to help! Glad it resolved your issue :)
– Sam Swift 웃
Jun 29 at 10:47






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV