php,mysql服务器已经消失
什么是错误的,当你在QUERY上连接到线路上的数据库时,你仍然得到“MySQL服务器已经消失”?php,mysql服务器已经消失
检查此示例代码:
mysql_connect("localhost", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
$sql = mysql_query("SELECT id FROM db");
while ($data = mysql_fetch_assoc($sql)) {
$ids[] = $data[id];
}
foreach ($ids as $id) {
$content = file_get_contents("http://www.id.com/?id=$id");
if (stristr($content, "the id is ok man")) {
mysql_connect("localhost", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
mysql_query("UPDATE db SET status = 'OK' WHERE id = '$id'");
}
}
MySQL服务器消失,我得到的是,在foreach循环。 顺便说一句我需要在foreachloop中连接,因为它可能需要一段时间才能找到更新的内容(比如1-2分钟),然后肯定我会得到mysql服务器已经消失。
我假设你的问题是在发送第一个UPDATE查询之前脚本可能执行很长时间。
您应该检查my.cnf中的wait_timeout值。你可以通过运行查询“SHOW VARIABLES;”来检查你的wait_timeout
您也可以尝试一段代码做了重新连接:
if (!mysql_ping ($conn)) {
//here is the major trick, you have to close the connection (even though its not currently working) for it to recreate properly.
mysql_close($conn);
$conn = mysql_connect('localhost','user','pass');
mysql_select_db('db',$conn);
}
与您的代码相结合将是:
mysql_connect("localhost", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
$sql = mysql_query("SELECT id FROM db");
while ($data = mysql_fetch_assoc($sql)) {
if (!mysql_ping()) {
//here is the major trick, you have to close the connection (even though its not currently working) for it to recreate properly.
mysql_close();
mysql_connect("localhost", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
}
$ids[] = $data['id'];
$content = file_get_contents("http://www.id.com/?id=$id");
if (stristr($content, "the id is ok man")) {
mysql_query("UPDATE db SET status = 'OK' WHERE id = '$id'");
}
}
同样的事情发生:(MySQL服务器已经消失 –
另外,我只是意识到,没有必要循环两次? –
尝试修改版本吗?另外,也许你的“file_get_contents”是非常慢? –
这周我们得到了错误,在这里工作是从MySQL的官方文档:http://dev.mysql.com/doc/refman/5.0/en/gone-away.html
这部分还包括查询错误期间服务器相关的连接丢失。
MySQL服务器最常见的原因已消失错误是服务器超时并关闭连接。
是使用相同的MySQL服务器和架构两个MySQL连接? – opps
我不认为你应该连接到整个脚本的数据库不止一次。 –
你如何得到你的foreach循环中的连接已经消失?这个脚本的输出是什么(错误信息)? – Rijk