哪种MySQL数据类型用于IP地址? [英] Which MySQL datatype to use for an IP address?
本文介绍了哪种MySQL数据类型用于IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
如何在mySQL中存储IP
Possible Duplicate:
How to store an IP in mySQL
我想从$_SERVER['REMOTE_ADDR']
和其他一些$_SERVER
变量中获取IP地址,哪种数据类型是正确的?
I want to get the IP address from $_SERVER['REMOTE_ADDR']
and some other $_SERVER
variables, which datatype is the right one for this?
是VARCHAR(n)
吗?
推荐答案
Since IPv4 addresses are 4 byte long, you could use an INT
(UNSIGNED
) that has exactly 4 bytes:
`ipv4` INT UNSIGNED
And INET_ATON
and INET_NTOA
to convert them:
INSERT INTO `table` (`ipv4`) VALUES (INET_ATON("127.0.0.1"));
SELECT INET_NTOA(`ipv4`) FROM `table`;
对于IPv6地址,您可以使用 BINARY
代替:
For IPv6 addresses you could use a BINARY
instead:
`ipv6` BINARY(16)
并使用 PHP的inet_pton
和 inet_ntop
进行转换:
And use PHP’s inet_pton
and inet_ntop
for conversion:
'INSERT INTO `table` (`ipv6`) VALUES ("'.mysqli_real_escape_string(inet_pton('2001:4860:a005::68')).'")'
'SELECT `ipv6` FROM `table`'
$ipv6 = inet_pton($row['ipv6']);
这篇关于哪种MySQL数据类型用于IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文