有什么不对,当你在QUERY之前连接到LINE上的数据库时,你仍然得到"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'");
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
结合您的代码将是:
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'");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8027 次 |
| 最近记录: |