/    Sign up×
Community /Pin to ProfileBookmark

Get ip_address out of array in php

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

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 02.2020 — To start with, $temp = $row ; returns an array, since that's what PDOStatement::fetch() returns. You probably mean $temp = $row['IP_ADDRESS'];.

Ideally, you probably could do one query to return all the data you need by leveraging some joins and/or sub-queries. I'm hesitant to suggest anything specific since I don't know the details of your data and actual desired functionality. It might be something like:
<i>
</i>select distinct
d.IP_ADDRESS,
i.country,
i.area,
i.city
from download as d
inner join ip_lookup as i on d.IP_ADDRESS between i.start_ip and i.end_ip
where d.FILENAME is not null
Copy linkTweet thisAlerts:
@larry29936authorMay 02.2020 — @NogDog#1618055 Thanks for the reply. You provided exactly what I needed to know about the array. I have a further question. I've recreated the tables with the ip's stored as int(50) unsigned. Do I need to modify the above code to use the MySQL functions INET_ATON and INET_NTOA in my queries?
Copy linkTweet thisAlerts:
@NogDogMay 02.2020 — @larry29936#1618056

If the IPs in both tables are stored as unsigned integers, you're good to go. If they are dot-separated strings in the DOWNLOAD table, then it looks like you could just convert it in the query:
<i>
</i>. . . on inet_aton(d.IP_ADDRESS) between i.start_ip and i.end_ip

(I've never worked with those functions, so that's just an educated guess.)
Copy linkTweet thisAlerts:
@larry29936authorMay 02.2020 — @NogDog#1618057 Thanks for that info. All ip fields are int(50) unsigned. I'm seeing what may be a new problem: All of the IPv6 ip's are coming into the start and end ip fields as 2. The IPv4 ip's are coming in as 2 to 6 digit numbers.
Copy linkTweet thisAlerts:
@larry29936authorMay 03.2020 — @NogDog#1618057 I ran your code to do the update and it took 7 minutes. Reset those fields to null and ran my code and it took 12 minutes. In both cases it was a total of 1300 records in downloads table to evaluate and update against 5M+ records in the ip_lookup table. There must be a faster way. I have indexes on all the ip fields.
Copy linkTweet thisAlerts:
@NogDogMay 04.2020 — Yuck. Did you have to use the inet_aton() function? If so, then it probably has to do a full table scan (and field value conversion) on that table to convert them all before doing the BETWEEN comparison. If, however, it's stored as an integer already and is indexed, I guess we might need to see some EXPLAIN results to find out where it's thrashing.
<i>
</i>EXPLAIN ANALYZE
SELECT -- rest of query here...
Copy linkTweet thisAlerts:
@ridhantaMay 05.2020 — wow, amazing[https://leo9studio.com/](url)
×

Success!

Help @larry29936 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...