mysql_connect第二次不起作用 [英] mysql_connect doesn't work second time
问题描述
未回答的重复项:如何在出现异常后重新连接php adodb:mysql服务器消失或查询过程中与MySQL服务器的连接失败
mysql_connect第一次工作,但此后再也无法工作...
mysql_connect works the first time, but never works after that...
$connectDb = mysql_connect(secret, secret, secret);
mysql_select_db("secret", $connectDb);
$sleepPeriod = 1800;
sleep($sleepPeriod);
while (true) {
$result = mysql_query("good query", $connectDb);
if (!$result) {
if (mysql_error()=='MySQL server has gone away') {
echo "MySql connection was disconnected... reconecting...\n";
$connectDb = mysql_connect(secret, secret, secret);
mysql_select_db("secret", $connectDb);
continue;
} else {
die("Invalid Query: ".__FILE__.':'.__LINE__.' '.mysql_error()."\n");
}
}
//DO STUFF
sleep($sleepPeriod);
}
如果发生超时或断开连接,则mysql_connect
似乎失败,并且mysql_error
不断返回"MySQL server has gone away"
,这将导致无限循环,可能持续数天.是否有其他方法可以清除mysql_error
的错误响应或使mysql_connect
再次运行,而不必手动重新启动此程序或使用cron.
if a timeout or disconnect happens mysql_connect
seems to fail and mysql_error
continually returns "MySQL server has gone away"
, which results in an infinite loop that can go for days. is there some other way to clear the error response of mysql_error
or to make mysql_connect
run a second time without having to restart this program manually or resorting to cron.
我刚刚注意到mysql_connect
有一个名为new_link
的奇怪的(愚蠢的?)参数,但是如果php的mysql代码在默认情况下故意在超时时禁用重新连接,那将是一个令人毛骨悚然的设计...无论如何,我都会进行测试回来.
I just noticed that mysql_connect
has a strange(stupid?) parameter called new_link
, however it would be an outrageous design if php's mysql code purposefully disables reconnects on timeout by default... I'll test regardless and get back.
推荐答案
mysql_connect
不在乎连接是否已断开连接或超时,除非您打电话,否则它将永远不会再次连接并使用参数new_link
和值true
.
mysql_connect
doesn't care if the connection has disconnected or timed-out it will never connect a second time unless you call it with the parameter new_link
with the value true
.
mysql_connect($server,$username,$password,true);
这篇关于mysql_connect第二次不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!