Php 迭代mysql结果以进行比较
这是检查服务器是否打开/关闭的脚本Php 迭代mysql结果以进行比较,php,mysql,Php,Mysql,这是检查服务器是否打开/关闭的脚本 $ip = "xxxxxxxx"; $port = "xxx"; $fp = @fsockopen("udp://$ip", $port, $errno, $errstr, 1); socket_set_timeout($fp, 000002); if (!$fp) { echo "OFFLINE"; } else { echo "ONLINE"; } @fclose($fp); mysql表 id | ip | po
$ip = "xxxxxxxx";
$port = "xxx";
$fp = @fsockopen("udp://$ip", $port, $errno, $errstr, 1);
socket_set_timeout($fp, 000002);
if (!$fp)
{
echo "OFFLINE";
}
else
{
echo "ONLINE";
}
@fclose($fp);
mysql表
id | ip | port | online_status |
1 | ip1 | port1 | |
2 | ip2 | port2 | |
3 | ip3 | port3 | |
4 | ip4 | port4 | |
5 | ip5 | port5 | |
我需要检查每个服务器是否同时打开/关闭,并在表中写入。我该怎么做
$servers = $db->getArray("SELECT * FROM servers");
尝试在阵列中循环并尝试连接到每台服务器。这将告诉您是否可以连接到服务器。然后,更新表以包含状态
/* Loop through the servers */
foreach($servers as $server)
{
$id = $server['id'];
$ip = $server['ip'];
$port = $server['port'];
/* Try to connect to this particular server */
$fp = @fsocketopen("udp://$ip", $port, $errno, $errstr, 1);
socket_set_timeout($fp, 000002);
$status = '';
/* Determine the status of the server */
if(!$fp)
{
$status = 'OFFLINE';
}
else
{
$status = 'ONLINE';
}
/* Close our connection */
@fclose($fp);
/* Update the servers table to show the status of the server */
$db->Update("UPDATE servers SET online_status='$status' WHERE id='$id'");
}