Hack Forums
[PHP] Why is this causing an infinite loop? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] Why is this causing an infinite loop? (/showthread.php?tid=5855572)



Why is this causing an infinite loop? - .eXe - Jun 29, 2018

I have a MySQLi statement in a function as follows:

PHP Code

<?php 
function getTimeData($time) {
    
$con = mysqli_connect("cptdb-enc.cvsafxbefwwm.eu-west-2.rds.amazonaws.com:3306", "root", "j7I4XaA6UnX1", "Iron") 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:

PHP Code

<?php 
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.


RE: Why is this causing an infinite loop? - Anxuein - Jun 29, 2018

Because you keep calling the function which is then repeating the query rather than continue. Try adding the _query outside of the function.


RE: Why is this causing an infinite loop? - .eXe - Jun 29, 2018

(Jun 29, 2018 03:20 AM)Anxuein Wrote:  Because you keep calling the function which is then repeating the query rather than continue. Try adding the _query outside of the function.

Yep that fixed it.

Thanks!


RE: Why is this causing an infinite loop? - Anxuein - Jun 29, 2018

You're welcome. Also since you're using SELECT * please see the answers on this SO question: https://stackoverflow.com/questions/3639861/why-is-select-considered-harmful


RE: Why is this causing an infinite loop? - .eXe - Jul 5, 2018

(Jun 29, 2018 03:47 AM)Anxuein Wrote:  You're welcome. Also since you're using SELECT please see the answers on this SO question: https://stackoverflow.com/questions/3639861/why-is-select-considered-harmful

Yeah, I used SELECT
as I'm using most of the columns later on, so would have had the same effect as if I'd done SELECT 1,2,3,4 etc.


RE: Why is this causing an infinite loop? - Mr. Miyagi - Jul 6, 2018

(Jun 29, 2018 03:09 AM)Evil.eXe Wrote:  I have a MySQLi statement in a function as follows:

PHP Code

<?php 
 function getTimeData
($time) {
 
   $con = mysqli_connect("cptdb-enc.cvsafxbefwwm.eu-west-2.rds.amazonaws.com:3306", "root", "j7I4XaA6UnX1", "Iron") 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:

PHP Code

<?php 
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.

I'm sure you're aware of this, but your database credentials are visible in the snippet of code you provided. Just a heads up if you didn't already know.


RE: Why is this causing an infinite loop? - .eXe - Jul 6, 2018

(Jul 6, 2018 07:37 AM)Mr. Miyagi Wrote:  I'm sure you're aware of this, but your database credentials are visible in the snippet of code you provided. Just a heads up if you didn't already know.

Aye. The connection isn't open to the public, but thanks nonetheless.