2

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.

1
  • The manual entry is a bit ambiguous in my mind. It might as well mean, it works everywhere, but on Windows only with PHP 5.3.1 or higher. Are you on a Linux machine? Can you try out? Commented Jan 12, 2010 at 20:24

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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
@Yada, Why are you posting the solution the question has already stated not to work? How can we make it work in Windows < PHP 5.3.1?
2

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

Comments

1

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

2 Comments

Edit your question and place what you placed here, there :)
Why did you post TWO answers to this thread?! You should "merge" them and delete one of them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.