-2

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ma77c
  • 1,052
  • 14
  • 31
  • 2
    I understand you are working with a legacy system, but why continue to write outdated code? What is stopping you from properly writing the new code you are working on? – GrumpyCrouton Mar 22 '19 at 18:53
  • 1
    I don't think your issue is from using `mysql_fetch_assoc`, the result from the query is exhausting your max allowed memory size. – GrumpyCrouton Mar 22 '19 at 18:55
  • The memory limit is because you are returning to much data, right? Fetchall would still have that issue. Are you selecting with `*` whereas you could just list some of the columns? Are you needing all data at once? Maybe paginate the results? – user3783243 Mar 22 '19 at 18:55
  • Is there raw image data in there or something? That's an awful lot for one row. Alternatively, are you running this within a loop and appending the results into an array each time through? – Patrick Q Mar 22 '19 at 18:56
  • @user3783243 `fetchall()` would still have that issue, but `fetch()` would not. I am not selecting with `*`. I am not sure how I would paginate the results as there is not an auto incremented primary key. – ma77c Mar 22 '19 at 18:57
  • 1
    For pagination, you can use the ```LIMIT [start row],[num_rows]``` clause in your query. – Sloan Thrasher Mar 22 '19 at 19:03
  • 2
    @TT4M.C `fetch()` also would if you did `while($row = fetch..){ $something[] = $row;}` because you'd be building an array of all results still. Maybe provide your actual code and give us an idea about what you are selecting. Without that we're all guessing – user3783243 Mar 22 '19 at 19:05
  • @SloanThrasher That is how I am doing it, if you want to make an answer. I will accept. – ma77c Mar 29 '19 at 13:20

1 Answers1

1

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.
Sloan Thrasher
  • 4,953
  • 3
  • 22
  • 40