开发者

dynamically change mysqli connection timeout in PHP

开发者 https://www.devze.com 2022-12-16 08:26 出处:网络
how can I dynamically change MySqli connection timeout using PHP? I found the following manual that you can set an option after a connection is opened, but it says it only support Windows since PHP 5.

how can I dynamically change MySqli connection timeout using PHP? I found the following manual that you can set an option after a connection is opened, but it says it only support Windows since PHP 5.3.1:

http://www.php.net/manual/en/mysqli.options.php

I'm using PHP5.2.4, MySQLi (improved version)

the default connection timeout is 20 seconds in my.cnf, but in a special php sc开发者_JAVA百科ript I have I'd like to set it to higher like 500 seconds before it times out.


$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 500)

manual

However, making a connection from php to mysql is NOT resource intensive. IMHO, it is a better idea to have a smaller timeout and make multiple connections. Doing so will free up the connections for other processes.


the correct solution for mysqli is right after an connection is opened, issue the following query to change session.wait_timeout to longer:

SET @@session.wait_timeout=500


I'm very sorry for the confusion, but I'm using Linux server, not Windows. I misunderstood the comment in the PHP docs "supported on Windows with TCP/IP since PHP 5.3.1", I thought that means the option is only supported on Windows server.

OK, so

$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 500);

will work perfectly for me.

Thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消