3

Fatal error: Maximum execution time of 120 seconds exceeded in...

$level = "1";

$get_question = $user_home->runQuery('SELECT * FROM questions WHERE Level = :Level ORDER BY RAND()');
$get_question->bindparam(":Level",$level);
$get_question->execute();
$fetch_question=$get_question->fetch(PDO::FETCH_ASSOC);

$stmtpr = $user_home->runQuery("SELECT * FROM used WHERE Last=:user_name");
$stmtpr->execute(array(":user_name"=>$fetch_question['Id']));
$rowpr = $stmtpr->fetch(PDO::FETCH_ASSOC);

while($stmtpr->rowCount() > 0)
        {
    $get_questionl = $user_home->runQuery('SELECT * FROM questions WHERE Level = :Level ORDER BY RAND()');
    $get_questionl->bindparam(":Level",$level);
    $get_questionl->execute();
    $fetch_question=$get_questionl->fetch(PDO::FETCH_ASSOC);
        }

Execution takes time only when it goes in loop.

user9836106
  • 105
  • 1
  • 2
  • 8

2 Answers2

2

The value of $stmtpr->rowCount() never changes, so your loop never ends. You're basically saying:

while (10 > 1) { ... }

It's not exactly clear what you're trying to do, but it appears you're trying to re-query the same rows you just queried. You probably just want to loop over the original result set.

Also note that ORDER BY RAND() is notoriously non-performant. Check this question for some alternative ideas.

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • Ok, I will look for alternatives.., you said **The value of `$stmtpr->rowCount()` never changes....** how can it be changed in my case? – user9836106 Jun 29 '18 at 19:33
1

Add at the top this in your code Unlimited execution time

ini_set('max_execution_time', 0);

Also, you can add unlimited memory usage

ini_set("memory_limit", "-1");

In your case also change the rowCount() like this

  $count = $stmtpr->rowCount();

while($count > 0)
        {
    $get_questionl = $user_home->runQuery('SELECT * FROM questions WHERE Level = :Level ORDER BY RAND()');
    $get_questionl->bindparam(":Level",$level);
    $get_questionl->execute();
    $fetch_question=$get_questionl->fetch(PDO::FETCH_ASSOC);
    $count--;
        }
Aram Grigoryan
  • 740
  • 1
  • 6
  • 24