0

I'm trying to display all the records from a database but it's just spamming around 1,000 until it finally reaches 30 second timeout. I've only got 3 records in the database, not sure why it's doing this?

while($news = $engine->fetch_array("SELECT * FROM `cms_news` ORDER BY `id` DESC"))
{
    echo 'lol<br>';
}

"lol" gets printed hundreds of times before finally timing out (execution time exceeded)

Here is the fetch_array function from the $engine class:

final public function fetch_array($sql)
{
    $result = $this->connection->query($sql);
    return $result->fetch_array(MYSQLI_ASSOC);
}
5
  • How many rows do you have? Could it be worth paginating? Commented May 27, 2018 at 15:10
  • 6
    As you would have noticed if you printed something relevant to the retrieved row instead of random nonsense, due to how fetch_array is written (first thing it does is to execute the query) it will always retrieve the same first row until it exceeds its time limit. Commented May 27, 2018 at 15:10
  • @Script47 I have 3 rows. Commented May 27, 2018 at 15:11
  • @fvu How would I got about fixing this? I've tried using fetch_assoc but still the same response. Commented May 27, 2018 at 15:11
  • 3
    Easy, don't execute the query in the fetch_array row if you ever want to be able to retrieve anything else than the first row. Commented May 27, 2018 at 15:12

1 Answer 1

1

A while loop will keep running as long as it's true. You will have to switch up the logic for how you're doing things, or as to what you're trying to achieve.

In your example, the while loop,

while($news = $engine->fetch_array("SELECT * FROM `cms_news` ORDER BY `id` DESC")) {

keeps being true, hence why it keeps running.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.