I'm creating a report page and can't figure out how to convert the array (row) to just get the ip_address. I'm a newbie to php and can't figure out how to accomplish this. Here's the code that I have so far:

<?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") ;
    }

All ip_addresses are stored as binary(64) and the errors I'm getting follow:

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

I'd appreciate any guidance you can provide or understandable tutorials you can point me to.

Larry

Get value from array by key $temp['IP_ADDRESS'] but use of between in your code is wrong - should be convert by INET_ATON

In your while loop, your variable $row is like a record with a column for each thing you are pulling. Therefore, it's a PHP array with an array field for each thing you're pulling. In this case, you're just pulling one field (ip_address) so it's an array with just one element.

$row['ip_address'] can be used to retrieve the IP address within your loop.

You want to modify row 20 to be $temp = $row['ip_address'];

Thanks to all who responded.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.