ip 地址(ipv4格式)
127.0.0.1
如果用 varchar
存储,需要占用 15 个字节
如果用 unsigned int
存储,只需要占用 4 个字节
表结构
create table log(
id int(10) unsigned not null auto_increment,
mid int(10) unsigned not null,
ip int(10) unsigned not null,
primary key(id)
) engine=innodb default charset=utf8
MySQL 操作
inet_aton("127.0.0.1"); //ip地址转长整型
inet_ntoa(2130706433); //长整型转ip地址
//插入
insert into log(mid, ip) values(1, inet_aton("127.0.0.1"));
//查询
select id, mid, inet_ntoa(ip) ip from log;
PHP 操作
$long = ip2long("127.0.0.1"); //ip地址转长整型
$ip = long2ip(2130706433); //长整型转ip地址
$maxIp = $long + 10
$insertSQL = "insert into log(mid, ip) values(1, $long)";
$selectSQL = "select id, mid, inet_ntoa(ip) ip from log where ip < $long and ip > $maxIp";
优点
- 节省空间
- 可以根据 ip 段 查询
- 提升查询效率
网友评论