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);

}

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.

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 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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值