0

Like a lot of PHP coder I'm not using mysql_close(). But I'm dealing with an increasing user base and I hit my first too many connections on my database yesterday.

Would it be really beneficial to mysql_close() early in my code so as a page still loading is not still connected to the db. Or is it just considered crappy optimization?

David 天宇 Wong
  • 3,724
  • 4
  • 35
  • 47

2 Answers2

0

It is not crappy optimization to close the connection if you no longer need it. If you are doing processing on the data that you get from the database and if you are not going to store the results in the database again, you can close the connection.

$db = connect(...);
$data =$db->run_query(...)->fetchAll();
// closing the connection here is good
$db->close();
foreach($data as $key=>$value){
   //process data
}
Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66
0

Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution.

Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used.

http://php.net/manual/en/function.mysql-close.php

I'm sure it wont make much of a difference. Instead you can try optimizing your database structure.

Gilly
  • 9,212
  • 5
  • 33
  • 36