2

When I create a function, like this, must I close the mysqli result set or is it closed automatically?

function return_true(){
    $query_res = $database->query("SELECT * FROM balance_sheet_rows");
    //some code here
    return true;
}

On this case, do I need to do $query_res->close() or is it closed automatically when a function returns the value?

There isn't duplicate question, because other pepole ask if it must close before a script ending. I ask if i must close before a function return a value !. When i have a function that execute a query, my var $res must be closed ? Documentation --> http://php.net/manual/en/mysqli-result.free.php

Gianlo
  • 35
  • 5
  • 1
    Closed automatically **when the PHP instance ends**, which is *usually* at the end of the page load. – random_user_name Dec 02 '16 at 20:56
  • 6
    You may also [reference this similar question](http://stackoverflow.com/questions/880885/is-closing-the-mysql-connection-important) – WEBjuju Dec 02 '16 at 20:57
  • i know that is closed automatically when PHP instance ends, but is closed also a function return a value ?? – Gianlo Dec 03 '16 at 16:53

1 Answers1

1

There is no close() method in mysqli_result class. Probably you meant mysqli::close method that closes opened database connection. So that connection will be closed automatically when a PHP script finishes its execution. However it is recommended to close it manually (in your case $database->close()) because this will return resources to PHP and MySQL, which can improve performance.

If you asked about allocated memory for result set. Then yes, you should free that memory manually in order to reasonably use resources of your system. However, it is not required.

Oleks
  • 1,633
  • 1
  • 18
  • 22
  • mysqli_result class has a method called free(), on object oriented style, the method free can invoked by close(). Documentation --> http://php.net/manual/en/mysqli-result.free.php – Gianlo Dec 03 '16 at 16:47
  • well, you asked about close() method. I've updated the answer – Oleks Dec 03 '16 at 17:14