问题:是否可以将二进制文件(varbinary(16)
)转换为IPv4&在SELECT
查询中使用MySQL按位运算的IPv6地址?如果是这样,怎么样?
我知道有一个MySQL INET6_NTOA()
函数,但它只适用于MySQL 5.6.3+。我知道PHP有inet_ntop()
函数,但如果可能的话我宁愿使用按位运算。
PHP:
/**
* createQueryParam
*
* Determines what MySQL function should be used based on MySQL version.
*
* @param $ip The visitor's IP address or ip field in database table.
* @access public
* @return string
*/
public function createQueryParam($ip, $select_query=FALSE)
{
# Set the Database instance to a variable.
$db=DB::get_instance();
# Get the database client version.
$client_version=$db->client_version;
# If the client version is 5.6.3+ use INET6_ATON.
if($client_version>=50603)
{
# If this is not for a select query.
if($select_query===FALSE)
{
return " INET6_ATON('".$ip."')";
}
# This is for a select query.
return ' INET6_NTOA(`'.$ip.'`)';
}
# Else we assume PHP has IPv6 support and use PHP's inet_pton() to convert the IP to a binary.
else
{
# If this is not for a select query.
if($select_query===FALSE)
{
# If IPv4 then use MySQL function.
if($this->getIPVersion()==4)
{
return " INET_ATON('".$ip."')";
}
else
{
# Supports IPv4 & IPv6 (if PHP has IPv6 supprot enabled).
return inet_pton($ip);
}
}
# NOTE: How to handle select queries for IPv6 if MySQL version is less then 5.6.3?
# Can a MySQL bitwise operation be used here?
# IPv4 Only.
//return ' INET_NTOA(`'.$ip.'`)';
# For IPv6 I could use PHP's inet_ntop() but we can't return it here.
}
} #=== End -- createQueryParam