0

I am building a script to display some 24 hour charts. I am attempting to make 12 charts. One 24 hour (last hour) chart for each five minute data set i have collected. I am running accross an issue where my mysql command will not update the $hour_position. I've read many while loop inside while loop questions and have tried to construct a foreach alternative, and also tried to reset the query array, but none of this has worked. Any comments or links to another thread that can solve this problem would be appreciated. Thanks in advance.

$hour_position = 00;
$htime = -1;
$date = date('Y-m-d H:i:s');
$date = strtotime($date);
$date = strtotime($htime." day", $date);
$date = date('Y-m-d H:i:s',$date);

while($hour_position < 60){
    $price_history_qry = mysqli_query($con,"SELECT * FROM `dbTable` WHERE `server_time` > '$date' AND EXTRACT(MINUTE FROM `server_time`) like $hour_position ORDER BY `server_time` ASC");
    while($result = mysqli_fetch_array($price_history_qry)){
        //Create a couple of arrays to build a chart. 
    }
    //Build chart here
    echo $chart;
    $hour_position = $hour_position +05;
}
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
Kaff1n8t3d
  • 81
  • 1
  • 8
  • Have you tried using [`str_pad()`](http://php.net/str_pad), as your `00` and `05` will default to `0` and `5`? ie. `$hour_position = str_pad(00,2,'0',STR_PAD_LEFT);` / `$hour_position = str_pad($hour_position +05,2,'0',STR_PAD_LEFT);` – Sean Feb 17 '15 at 05:04
  • I haven't, I will try it. I actually haven't heard of that one. – Kaff1n8t3d Feb 17 '15 at 05:14
  • unfortunately this did not work either. – Kaff1n8t3d Feb 17 '15 at 05:35
  • Turns out i had a mysqli_close($con) in the //build chart here section of my code. When i removed that the loops work fine. I discovered this by testing the loop with basic variables and slowly adding in my previous code. Unfortunately the problem wasn't listed in my submitted code, but problem is solved. Sorry for the goose chase. I will look at this kind of troubleshooting before i submit for help again. – Kaff1n8t3d Feb 18 '15 at 04:37

2 Answers2

0

Try putting $hour_position in quote like

      ' $hour_position ' 

or you can use

      ". $hour_position ." 
H. Mahida
  • 2,356
  • 1
  • 12
  • 23
0

Your query has a small mistake. The variable inside single quotes will not be evaluated as variable. Check this. you need to change the select query like below.

$price_history_qry = mysqli_query($con,"SELECT * FROM `dbTable` WHERE `server_time` > '".$date."' AND EXTRACT(MINUTE FROM `server_time`) like ".$hour_position." ORDER BY `server_time` ASC");

And i haven't find any update query in your code. As you said you are using the update query inside while loop. So first check whether the variable has the value and then check whether it entering to while loop like below.

    $result = mysqli_fetch_array($price_history_qry)    
    var_dump($result);    
    while($result){
           //Create a couple of arrays to build a chart. 
           var_dump("entered");
   }

Try to use var_dump() function to debug.

Hope it helps you.

Community
  • 1
  • 1
Mahendran Sakkarai
  • 8,381
  • 6
  • 44
  • 66
  • Thanks for the help (everyone). @mahendran, I have tried multiple variations of quotes (and just tried your suggestion again) and it doesn't change. I have used var_dump to find out that my query variable $hour_position is not updating inside the SELECT query. I may have mispoken earlier. I am not using an UPDATE query. I am trying to update a variable in my query. I am trying to pull x data from database, process it, display it , then x + 05. Loop back through query pulling x +05 from db, process it, display it, then x +05 again...until I reach 60. – Kaff1n8t3d Feb 17 '15 at 15:11