php连mysql的诡异问题:“MySQL server has gone away”

今天写了个小程序
用来监测某些机器上的某些进程是否存在
需要从mysql中查出ip和进程字串
然后从中控上一台一台ssh上去ps查进程是否存在
如果状态变化(原来标记不存在的现在有了,或是原来标记存在的现在没有了)
还需要更新mysql库中的相应状态字段
因为查询出来的数据比较多(600多条)
而且我还要一台一台ssh上机器检测进程
等我发现某台机器的某个进程状态改变要update库的时候
数据库报错:

MySQL server has gone away

查了好些资料
也没找出原因来
最后按照文档上建议的
先判断
然后再执行mysql_query

function connect(){
$db = mysql_connect(’xxx.xxx.xxx.xxx’, ‘xxx’, ‘xxxxxx’);
mysql_select_db(’db1′);
return $db;
}
if (!$db){
mysql_close($db);
$db = connect();
}elseif(!mysql_ping($db)){
mysql_close($db);
$db = connect();
}
mysql_query(”$sql”);

不管怎么样
这个问题算是解决了
不过说实话:为什么我还是没搞明白
:)

Discussion Area - Leave a Comment




*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word