1

I have this configuration: MySQL: 5.1.66-0+squeeze1-log, PHP: 5.3.3-7+squeeze15,

I'm working wtih ipv6 address 2001:35c::/40

While I'm manipulating it on php( inet_pton, inet_ntop) it is good, but after I put it into MySQL database (VARBINARY(16) field, it get messed up (200103000000000000000000000000 instead of 2001035c0000000000000000000000). Where could be the problem?

$addr = '2001:35c::/40';

$addr = explode('/', $addr);
$address = $addr[0];

$address = inet_ntop(inet_pton($address));

$sql = "INSERT INTO IPv6 (IP, Mask, Parent_ID) VALUES ('".inet_pton($address)."', '".$addr[1]."', 0)";
$DB=new Database();
$DB->query($sql);

Also i spotted, that where there are 35c parti in address (ipv6) it get messed up after inserting into MySQL

4

2 回答 2

3

在插入之前尝试引用您的数据。

MySQL

mysql_real_escape_string(inet_pton($address))

PDO

$db->quote(inet_pton($address))

此方法还转义二进制数据,0 变为 \0 等。

@hamza-dzcyberdev 我猜三重转换是为了获得最短的 IPv6 符号。0000:0000 可以缩短为 ::, 0000: 到 0: 在这种情况下不介意,但在其他情况下可能会。

于 2013-05-27T08:42:38.390 回答
0

除了引用'and mysql_real_escape_string()or quote(),您还可以使用0x这样查询看起来像

INSERT INTO IPv6 (IP, Mask, Parent_ID) VALUES (0x2001035c0000000000000000000000, 40, 0).

但是,我不确定如何在 PHP 中处理这个问题。也许(但不能保证!)这样:

$sql = "INSERT INTO IPv6 (IP, Mask, Parent_ID) VALUES (0x".bin2hex($address).", '".$addr[1]."', 0)";

根据$addr来自哪里,$addr[1]也必须引用。

于 2013-05-27T11:17:23.203 回答