Php Mysqli Persistent connection error
I have a problem in my script that appen sometimes,usually when i leave it for some time(like 5-10 minutes)without requests i get this error:
Warning: mysqli_connect() [function.mysqli-connect]: MySQL server has gone away in FILE.php on line 30
and this is the code that gives me error is this:
$this->db=mysqli_开发者_如何学Pythonconnect('p:'.$this->db_host,$this->db_user,$this->db_pwd,$this->db_name);
The code is contained on a singleton class and i'm currently developing on windows7 with
- Apache 2.2.11
- MySQL 5.1.36
- PHP 5.3.0
(i use WampServer 2.0i)but i'll obviously use linux on the final server.
Thanks.
It easy... PHP 5.1 doesn't support it
http://php.net/manual/en/mysqli.persistconns.php
requires PHP 5.3
Are you checking for errors after you connect? That might reveal the need for a reconnection. Though looking at the documentation it appears there's no way to force mysqli to create a new connection.
You could use mysqli_ping()
to check the server before each call to the database but that's kind of a pain if you have a bunch.
The other thing to do if you admin the MySQL server or are on good terms with the person who does, is to increase the idle timeout on the server itself.
Personally IMHO, persistent connections are buggy in PHP and unless you have a very high-traffic site the increase in speed isn't enough to make it worth the trouble.
Check timeout values in your mysql server especially connect_timeout and wait_timeout.
When Connection closes by timeout you need to reconnect -> that should be done by your application.
精彩评论