0

I keep seeing Waiting for Cache and it takes at least 30 minutes to get the response. Is there a better way?

Here's the link: http://tapnapp.co/planning/phpScripts/getFullViewTasks.php?counter=1&sDateHour=1525154400&eDateHour=1527793200

And here is the code:

<?php

if (isset($_GET['counter'])) {
    require_once '../functions.php';

    $sDateHour = $_GET['sDateHour'];
    $eDateHour = $_GET['eDateHour'];

    $now = time() - 24*60*60;
    $fn = new Func;
    $fn->select('id_profile, groupe','profile',''," date_f_contract>='$now' AND groupe>0");

    $sel = new Func;
    $getHrName = new Func;

    while ($row = mysqli_fetch_assoc($fn->select)) {
        $pr_id = $row['id_profile'];

        $sel->select('*','counter',''," from_interval>='$sDateHour' AND to_interval<='$eDateHour' AND id_profile='$pr_id'");
        $nr= mysqli_num_rows($sel->select);

        if ($nr > 0) {
            //here we have to do the total counter
            $totalCounter = 0;
            $from_interval = 0;
            $to_interval = 0;
            $name = "";

            $tmp_id_profile = 0;
            $tmp_f_int = 0;
            $tmp_to_int = 0;

            while ($r = mysqli_fetch_assoc($sel->select)) {
                $frint = $r['from_interval'];
                $toint = $r['to_interval'];
                //$counter_ct = $r['counter'];
                $totalCounter += $counter_ct;

                $getHrName->select('libelle AS lbl, COUNT(libelle) AS totalHr',"horraire",""," date_heure_deb>='$frint' AND date_heure_fin<='$toint' AND id_profile='$pr_id' GROUP BY libelle ORDER BY totalHr DESC LIMIT 1");
                $rr = mysqli_fetch_assoc($getHrName->select);
                $r['hrname'] = $rr['lbl'];
                $name = $rr['lbl'];
                $r['counter'] = $rr['totalHr'];
                /*
                $from_interval=$frint;
                $to_interval = $toint;
                */
                $row['tasks'][] = $r;
            }
            /*
            $r['counter']=$totalCounter;
            $r['from_interval']=$from_interval;
            $r['to_interval']=$to_interval;
            $r['hrname']=$name;*/
            //$row['tasks'][]=$r;
        } else {
            /*
            id_counter: "1",
            id_profile: "17",
            from_interval: "1519887600",
            to_interval: "1519934400",
            counter: "18"}
            */

            /*
            $fake['id_counter']=0;
            $fake['id_profile']=$pr_id;
            $fake['from_interval']=$sDateHour;
            $fake['to_interval']=$eDateHour;
            $fake['counter']=0;
            $row['tasks'][]=$fake;
            */
        }
        $res[] = $row;  
    }

    $temp['data'] = $res;
    $fn->deconnect();
    $sel->deconnect();
    echo json_encode($temp, JSON_PRETTY_PRINT);  
}

?>

So what is wrong with this if anyone can understand? Is that because of mysql or because of the php?

Nic3500
  • 8,144
  • 10
  • 29
  • 40
DuliNini
  • 181
  • 2
  • 14
  • 1
    It's really hard to tell, especially since we don't know your data set. Try some logging to see where it's going, how long it takes, etc. – aynber May 31 '18 at 15:20
  • 1
    This is a very difficult question to answer and the only thing I can suggest is by logging different parts of your code to find the bottle neck. Start with the queries imo. This may help you on your debugging adventure: https://stackoverflow.com/questions/8310487/start-and-stop-a-timer-php – IsThisJavascript May 31 '18 at 15:20
  • 1
    This question is basically: https://stackoverflow.com/q/8291366/1531971 –  May 31 '18 at 15:25
  • i would advise indexing the table and joining both queries into one so no PHP looping is required.. Also you need to protect against SQL injections which looks like it's possible in your current code.. – Raymond Nijland May 31 '18 at 15:33

0 Answers0