Question

I'm trying to interact with a Piwik database that we installed on our server. Inside the Piwik database, the value for a stored ip address is inserted with inet_ntop(). I'm trying to decode those values and pull them out with inet_pton() so I can run a query to find a customers ip address in the Piwik database that's been stored in our local database.

The issue comes when I'm building the query. I take the stored address in the db and run it through inet_pton() like so...

$data = mysql_fetch_assoc(mysql_query("SELECT ip_address FROM data_table WHERE id = 1"));

$more_data = mysql_fetch_assoc(mysql_query("SELECT location_ip FROM piwik_log_visit WHERE location_ip = '".inet_pton($data['ip_address'])."'"));

Where the problem comes in is inet_pton($data['ip_address']) will display random characters (sometimes) included along with black-diamond question marks. It comes back with a mysql_error that says the query is invalid (because of the bad characters). I tried adding mysql_set_charset("utf8"); before the query was run with no (good) results.

Any idears?

Thanks!

Was it helpful?

Solution

$data = mysql_fetch_assoc(mysql_query("SELECT ip_address FROM data_table WHERE id = 1"));
$hexip = bin2hex(inet_pton($data['ip_address']));
$more_data = mysql_fetch_assoc(mysql_query("SELECT location_ip FROM piwik_log_visit WHERE hex(location_ip) = '$hexip'"));

In order to have mysql and php not throw an error, you need to convert the binary output of inet_pton to hex and then compare the mysql stored hex(location_ip) value to the $hexip variable.

OTHER TIPS

Binary story:

$bin_ip = '0x'.bin2hex(inet_pton($data['ip_address']));

"SELECT location_ip FROM piwik_log_visit WHERE hex(location_ip) = $bin_ip"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top