MYSQL - SELECT IP v4 / v6,inet_pton& BIN2HEX [英] MYSQL - SELECT IP v4/v6, inet_pton & bin2hex
问题描述
ip可以是ipv6和v4。
Table:User {
...
ip binary(16)
}
$ ip ='192.168.10.115';
$ ip = bin2hex(inet_pton($ ip)); //返回c0a80a73
$ result = $ this-> db-> select(SELECT * FROM User WHERE HEX(ip)= $ ip);
// $结果为空,因为在db中它存储为:
// HEX(ip)= C0A80A73000000000000000000000000
我如何获得与* 00000 *可行的匹配?
如果输入是一个ipv6匹配,这将是可以的,但ip v4不是。
VARBINARY
而不是 BINARY
?
如果检索到的值必须与为
存储器指定的值没有填充值相同,最好使用VARBINARY或
之一的BLOB数据类型。
I'm having bit of a trouble selecting correct values from my mysql sql server.
The ip can be ipv6 and v4.
Table: User{
...
ip binary(16)
}
$ip = '192.168.10.115';
$ip = bin2hex(inet_pton($ip)); // Returns c0a80a73
$result = $this->db->select("SELECT * FROM User WHERE HEX(ip) = $ip");
// $result empty because in db its stored as:
// HEX(ip) = C0A80A73000000000000000000000000
How can I get a viable match to the * 00000 * ?
If input was a ipv6 match this would be ok, but ip v4 not.
Can you use VARBINARY
instead of just BINARY
?
From the MySQL Manual on Binary/Varbinary:
If the value retrieved must be the same as the value specified for storage with no padding, it might be preferable to use VARBINARY or one of the BLOB data types instead.
这篇关于MYSQL - SELECT IP v4 / v6,inet_pton& BIN2HEX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!