Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

Why is this causing an infinite loop?

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.
aint my sig design skills sick?
Because you keep calling the function which is then repeating the query rather than continue. Try adding the _query outside of the function.

hi
(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!
aint my sig design skills sick?
You're welcome. Also since you're using SELECT * please see the answers on this SO question: https://stackoverflow.com/questions/3639...ed-harmful

hi
(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/3639...ed-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.
aint my sig design skills sick?
(Jun 29, 2018 03:09 AM).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.
(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.
aint my sig design skills sick?


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by HoneybunMay 10, 2018 11:52 AM 5 162 May 10, 2018 11:52 AM
Last Post: Honeybun
[PHP] by imrootSep 28, 2016 04:23 PM 2 215 Sep 28, 2016 04:23 PM
Last Post: imroot
[PHP] by theAbsoluteJul 24, 2014 11:57 AM 12 289 Jul 24, 2014 11:57 AM
Last Post: chrisn