我正在尝试编写代码以从表中获取ip并ping并插入ip和响应时间另一个表。 我写的内容如下。它回显ip和响应时间,但只有ips插入到表中。响应时间是空白的。
模式
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(6) | NO | PRI | NULL | auto_increment |
| ip | varchar(30) | NO | | NULL | |
+-----------+-------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+-------------------+-------+
| Log_time | timestamp | NO | | CURRENT_TIMESTAMP | |
| ip | varchar(30) | NO | | NULL | |
| Ping_time | varchar(30) | YES | | NULL | |
代码
<?php
// Connects to your Database
mysql_connect("localhost","xxx","xxx");
//database connection
mysql_select_db("xxxxx") or die(mysql_error());
$sql="select * from iplist where id=(SELECT MAX(id) FROM iplist)";
$data = mysql_query($sql) or die(mysql_error());
//asing ip to variables
$row = mysql_fetch_assoc($data);
$id=$row["id"];
$id1=$id;
while($id1>0) {
$sql="select ip from iplist where id=($id1)";
$data = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($data);
$ip=$row["ip"];
//ping ips
$ip_ad=$ip;
$ping = system("ping -c 1 $ip_ad | head -n 2 | tail -n 1 | awk '{print $7}'", $ping_time);
print $ping_time[0]; // First item in array, since exec returns an array.
$responcetime = $ping_time[0];
$dilay=$responcetime;
echo $ip;
echo $dilay;
//send data to database
//inserting data order
$order = "INSERT INTO Ping_log (ip, Ping_time) VALUES ('$ip','$dilay')";
//declare in the order variable
$result = mysql_query($order); //order executes
$id1 --;
}
?>