使用PHP在MySQL数据库中存储的IP地址 [英] IP address storing in MySQL database using PHP

查看:245
本文介绍了使用PHP在MySQL数据库中存储的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mysql中IP地址的正确字段类型是什么?什么是使用PHP存储它的正确方法?

what is the right field type for IP address in mysql? and what is the right way of storing it using PHP?

推荐答案

本教程可能对您有所帮助。

保存IPv4地址的最有效方法是一个INT字段(不是你想象的VARCHAR)。您可以使用PHP的 ip2long 转换它们,然后使用MySQL的 INET_NTOA 功能或PHP的 long2ip 功能。

The most efficient way of saving IPv4 addresses is with an INT field (not VARCHAR as you might expect). You convert them using PHP's ip2long and back using either MySQL's INET_NTOA function or PHP's long2ip function.

如果您需要存储IPv6,则需要使用BINARY字段而PHP inet_pton 功能。

If you need to store IPv6, you'll want to use a BINARY field instead and PHP's inet_pton function.

这篇关于使用PHP在MySQL数据库中存储的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆