21

I was looking through my code and read that it was recommened to use mysqli_free_result when your result object is not needed anymore. But after seeing that each query is outputed to the $result variable repeatedly throughout the script, I am wondering if mysqli_free_result would really be necessary. Seems like each time a query is ran the $result variable is already being wiped clean and set to a new result. Just curious if anyone has any input on this.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
JoJo
  • 221
  • 1
  • 2
  • 5

2 Answers2

9

Actually it’s necessary, because it might make a heavy load into the server when many requests are made. So preferably, you should use it.

Some other cases when you know that this query is followed by other query so you don’t have to use it.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
mamdouh alramadan
  • 8,349
  • 6
  • 36
  • 53
  • 1
    Do I need to use `mysqli_free_result($result)` if `$result` is getting out of scope? Like when function containing `$result` ends? Another way to ask this question would be: can `unset($result)` be used instead of `mysqli_free_result($result)`? – MacDada Nov 30 '17 at 21:47
  • @MacDada when a variable get GCed, it's memory value is cleaned, however, that doesn't mean the GC is looking for the purpose of that variable nor does it do additional clean up. Hence, Yes, I would still do the call – mamdouh alramadan Dec 31 '17 at 16:00
  • PHP absolutely does look at variable types when doing GC. From my testing, `mysqli_result` variable going out of scope has the same effect on memory as caling `free()`, so you don't need to call it if the variable gets GC'd anyway. – Shira Oct 03 '22 at 20:19
9

It’s never strictly necessary, but it's good practice to keep an eye on resources you’re using and know when you don't need them anymore.

It’s pretty minimal effort to drop in that extra line of code, so I would just do it every time you're finished with a result set. It has the added bonus of making it clear to someone reading your code when you’ve finished with a resource.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Michael Mior
  • 28,107
  • 9
  • 89
  • 113
  • 3
    Even if there are repeated queries and the $result variable is already being wiped clean and set to a new result? – JoJo Dec 30 '12 at 04:16
  • 4
    Yes. You won't lose anything by making things explicit (except having to type an extra line of code). But what happens if later some of your code gets copied somewhere else where there isn't anything done with `$result` after? Or a refactoring messes with the order of things? – Michael Mior Dec 30 '12 at 04:18
  • OK, thanks for the help. I was just curious if using mysqli_free_result after each query result would cause more unecessary load to the server since the results are automatically deleted and reset. Except for the last one of course. But then the script is almost completed anyway and all results are automatically freed. – JoJo Dec 30 '12 at 04:22
  • If the result is freed automatically anyway, calling `mysqli_free_result` is going to do very minimal extra work, so it won't be a performance issue. – Michael Mior Dec 30 '12 at 04:27