我试图创建一个cronjob脚本,它检查一些服务器的状态。它只使用我表中的第一个ip。
$aQ = db::aQuery("SELECT hostIp FROM `gh_server_hosts`");
$a = mysql_fetch_array($aQ);
foreach ($a as $value) {
$timeout = "10";
$ssh= @fsockopen("$value","22",$timeout);
if($ssh) {
$status = 1;
}
else {
$status = 0;
}
$time = time();
if(db::aQuery("UPDATE `gh_server_hosts` SET hostLastScanned='".$time."', hostLastStatus='".$status."' WHERE hostIp = '".$value."'")) {
echo "scanned";
} else {
echo "error";
}
}
您必须将mysql_fetch_array
放入循环中以获得这样的所有记录:
$aQ = db::aQuery("SELECT hostIp FROM `gh_server_hosts`");
while ($value = mysql_fetch_assoc($aQ)) {
$timeout = "10";
$ssh= @fsockopen($value['hostIp'],"22",$timeout);
// Rest of your code
}
我使用mysql_fetch_assoc
而不是mysql_fetch_array
来限制输出数组。此外,正如其他人提到的mysql_*
函数被弃用,所以请在PHP 5.5+上运行此代码之前更新您的代码…
mysql_fetch_assoc()
可以代替mysql_fetch_array()
,但请注意mysql的弃用警告。(除了安全问题,如果PHP升级到7.0,您的代码将失败)。