views:

416

answers:

5

Hi, 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.

A: 

The solution seems to be to use another server than Windows (e.g. Linux, BSD or some other real operating system), or to upgrade to PHP 5.3.1.

Emil Vikström
+1  A: 
$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.

Yada
His problem (as far as I understand) is that this won't work on his Windows server because he is using PHP < 5.3.0.
Pekka
I agree, all my normal connections are subject to 20 second timeout. but this specific script takes long time to run and sometimes idle mysql connection for long, that's why I need longer connection just for this script
A: 

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

Ok, so

$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 500)

will work perfectly for me.

Thanks

Edit your question and place what you placed here, there :)
AntonioCS
A: 

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

SET @@session.wait_timeout=500

A: 

Ulf Wendel, from the MySQL and PHP development teams, has a nice blog posting about this: http://blog.ulf-wendel.de/?p=273

johannes