Search code examples
phpfetchphp-5.4

Fetch vs FetchAll with mysql_query | PHP


I am aware that using mysql_query is terrible and the much better / preferred way of doing this is like so:

PREFERRED

$dbquery->fetch(PDO::FETCH_ASSOC))

BUT I HAVE TO

However, working with a legacy system that uses mysql_query ... running into a PHP Fatal error: Allowed memory size of 536870912 bytes exhausted error when calling mysql_fetch_assoc.

QUESTION

Does msyql_query / mysql_fetch_ have functionality that acts as fetch vs fetchAll?


Solution

  • Two things:

    1. Use a loop in PHP and read one row at a time. (not needed if you use #2)
    2. Use the LIMIT {start row},{number of rows} clause in your SQL statement for pagination.