37

Just started using mysqli. If I'm working with small data sets on small websites (traffic-wise), do I really need to use these all the time?

$result->close(); 
$mysqli->close();

Also, for someone doing custom PHP and MySQL work without a framework, is mysqli the general preferred way of interacting with MySQL?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
James
  • 379
  • 1
  • 4
  • 4

4 Answers4

41

PHP will close all open files and DB connections at the end of the script. It's good practice to do it manually when you are done with the connections, but it's no disaster if you don't. If you have a DB connection that will be used throughout the whole script you can as well leave it open.

+1 on PDO

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
18

According to current documentation, you should always use $mysql->kill() in addition to $mysql->close().

$thread = $mysqli->thread_id;
$mysqli->kill($thread);
$mysqli->close();
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ixalmida
  • 603
  • 5
  • 15
  • 4
    as mentioned before, it's good practice to do your own clean up, but the documentation, as far as I know, at the time of writing this, doesn't mention that you should use it. +1 for good practice. – PatomaS Oct 20 '12 at 04:59
  • 3
    The PDO claim seems dubious at best to me, and I'm pretty sure you'd see recent timestamps on PDO related files in their GitHub repo. Have you got hard evidence to back it up? – GordonM Dec 24 '13 at 21:21
  • 4
    Additionally to what @GordonM mentions, reference in the answer is missing about the claim that *"you should always use $mysql->kill()"*. – hakre Dec 24 '13 at 21:25
  • 1
    @hakre if he is talking about the documentation [mysqli::close](http://www.php.net/manual/en/mysqli.close.php) It's just a comment in the Dictionary and it's been downvoted 8 times "You should always use mysqli_kill() function before mysqli_close() to actually close and free up the tcp socket being used by PHP. Garbage collection after script execution nor mysqli_close() do not kill the tcp socket on their own." – DJ22T Apr 30 '14 at 15:51
  • 1
    Grain of salt...be aware that I was referring to an old PHP version's documentation. I'm sure connection handling has changed somewhat since then. Also, I'm now a big advocate of Laravel, so I rarely think about connection life-cycle anymore. It would be a piece of cake to compare PDO to MySQLi connection times simply by swapping out the connection in the ".env" file, though I have no real desire to try it. – Ixalmida Apr 09 '15 at 21:47
  • "should always use mysqli_kill() function *before* mysqli_close()" makes the code snippet above opposite :o. – dyasta Apr 18 '17 at 13:14
  • ***$mysqli->close();*** should be located at the very end! (***AFTER:*** $mysqli->kill($thread_id);) See: http://php.net/manual/en/mysqli.kill.php – Tarik Aug 15 '17 at 05:21
3

You should get in the habit of doing cleanup right (calling close as soon as you're done), or the resource leaks can gradually accumulate until they impact performance.

As far as what DB layer, learning PDO should be worthwhile because it is well-designed and compatible with all the major databases.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • +1 to good practices and doing clean up. -1 to pdo. If there's no real need for it, just adds another layer of compexity. – PatomaS Oct 20 '12 at 04:54
  • If the only reason to use PDO is compatibility with other databases then that reason is moot for those of us who never and will never use a different database. – Vincent Feb 26 '19 at 00:16
2

It is a good practice to release resource early when it is no more needed, this may avoid resource peek out when there are more number of concurrent user accessing the same page

Raj
  • 1,156
  • 11
  • 15