问:我正在创建报告页面,无法弄清楚如何将数组(行)转换为仅获取ip_address。我是php的新手,无法弄清楚如何完成此操作。这是我到目前为止的代码:
<?php
$filename = NULL;
session_start();
// start of script every time.
// setup a path for all of your canned php scripts
$php_scripts = '../php/'; // a folder above the web accessible tree
// load the pdo connection module
require $php_scripts . 'PDO_Connection_Select.php';
if (!$con = PDOConnect("test")):
{
echo "Failed to connect to database" ;
exit;
}
else:
{
$stmt = $con->query("SELECT DISTINCT(IP_ADDRESS) FROM download WHERE FILENAME is not NULL");
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$temp = $row ;
echo var_dump($temp); // shows as "array(1) { ["IP_ADDRESS"]=> string(64) "92.240.60.16" }"
$country = $con->query("SELECT (country) FROM ip_lookup WHERE '$temp' between start_ip and end_ip");
$area = $con->query("SELECT (area) FROM ip_lookup WHERE '$temp' between start_ip and end_ip");
$city = $con->query("SELECT (city) FROM ip_lookup WHERE '$temp' between start_ip and end_ip");
$test = $con->query("UPDATE TABLE download SET country = '$country', area = '$area', city= '$city' WHERE IP_ADDRESS = '$temp' and FILENAME is not NULL") ;
}
所有ip_addresses都存储为binary(64),并且出现的错误如下:
Notice: Array to string conversion in /home/larry/web/test/public_html/report.php on line 26
Notice: Array to string conversion in /home/larry/web/test/public_html/report.php on line 27
Notice: Array to string conversion in /home/larry/web/test/public_html/report.php on line 28
Recoverable fatal error: Object of class PDOStatement could not be converted to string in /home/larry/web/test/public_html/report.php on line 29
感谢您可以提供的任何指导或可以为我提供的易懂的教程。
答:通过键从数组中获取值,$temp['IP_ADDRESS']但是between在代码中的使用是错误的-应该通过INET_ATON
答:在while循环中,变量$ row就像一条记录,其中包含您要拉动的每个对象的列。因此,这是一个PHP数组,其中包含要提取的每个对象的数组字段。在这种情况下,您仅提取一个字段(ip_address),因此它是一个只有一个元素的数组。
$row['ip_address'] 可用于检索循环中的IP地址。
答:您想要将第20行修改为 $temp = $row['ip_address'];