views:

98

answers:

2
$con = mysql_connect("localhost:".$LOCAL_DB_PORT, $LOCAL_DB_USER, $LOCAL_DB_PASS);
mysql_select_db("hr", $con);
mysql_query("set names utf8", $con);

while(true)
{
    do_stuff($con);
    sleep(50);
}

If connection timesout in 50 seconds,will $con still work?

A: 

If the connection times out, it won't work.

To answer the question from the comment, to cope with the problem, refer to php.net manual page for mysql_connect() which says:

If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned.

so if you want to make sure you always have an open connection, just try to open a new one with the same arguments after the code you substituted with sleep() is done executing.

code_burgar
Then how to cope with it?
Mask
+1  A: 

Simple answer: why don't you try it and see?

I believe codeburger is correct: if the MySQL connection times out, then it's gone. You could use a persistent connection with mysql_pconnect. You will need to call that function after sleep each time but it will use an existing connection, saving overhead.

DisgruntledGoat
you don't even need to use a persistent connection. subsequent calls to mysql_connect() with the same parameters will use a previous connection within the same execution of the PHP script. so if mysql disconnects, you get a new connection. if you're not disconnected, nothing happens.
longneck
True, but my point was connecting with `mysql_pconnect` uses less overhead each time.
DisgruntledGoat