错误字符inet_pton() PHP函数*不Zend* Piwik location_ip问题


Bad Characters with inet_pton() PHP Function *Not Zend* Piwik location_ip issue

我正在尝试与我们安装在服务器上的Piwik数据库进行交互。在Piwik数据库中,使用inet_ntop()插入存储的ip地址的值。我正试图解码这些值,并将它们与inet_pton()拉出来,这样我就可以运行查询,在Piwik数据库中找到存储在我们本地数据库中的客户ip地址。

问题来了,当我建立查询。我采取存储在数据库中的地址,并通过inet_pton()运行它,像这样…

$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'])."'"));

问题出现的地方是inet_pton($data['ip_address'])将显示随机字符(有时)与黑菱形问号一起包含。它返回一个mysql_error,表示查询无效(因为错误字符)。我试着添加mysql_set_charset("utf8");在查询没有(好的)结果之前。

有什么想法吗?

谢谢!

$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'"));

为了让mysql和php不抛出错误,您需要将inet_pton的二进制输出转换为十六进制,然后将mysql存储的十六进制(location_ip)值与$hexip变量进行比较。

二进制故事:

$bin_ip = '0x'.bin2hex(inet_pton($data['ip_address']));
"SELECT location_ip FROM piwik_log_visit WHERE hex(location_ip) = $bin_ip"