Beier
Beier

Reputation: 3157

dynamically change mysqli connection timeout in PHP

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 script I have I'd like to set it to higher like 500 seconds before it times out.

Upvotes: 2

Views: 15140

Answers (3)

Beier
Beier

Reputation: 3157

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

Upvotes: 2

Beier
Beier

Reputation: 3157

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

Upvotes: 1

Yada
Yada

Reputation: 31225

$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.

Upvotes: 2

Related Questions