PHP Equivalant for INET_NTOA and INET_ATON
You want ip2long()
and long2ip()
.
$ip = '192.0.34.166';
printf("%u\n", ip2long($ip)); // 3221234342
As it notes in the manual:
Note: Because PHP's integer type is
signed, and many IP addresses will
result in negative integers, you need
to use the "%u" formatter of sprintf()
or printf() to get the string
representation of the unsigned IP
address.
Fatal error: Call to undefined function INET_ATON()
inet_aton()
is a Mysql function. In PHP, you'd use inet_ntop()
and inet_pton()
.
Can't echo rows using INET_NTOA
change your sql to use an alias, eg:
$sql="SELECT `subnet_id`, INET_NTOA(`address`) AS inet, `default_gateway`, `notes`, `site_id`, `user_id`, `mask_id` FROM `subnets`";
and then you should be able to fetch the field using
<td class="viewDataWidth"><?php echo $rows['inet']); ?></td>
your first attempt didn't work since you tried to call a php function "INET_NTOA" instead of retrieving the result from the mysql function. you could do that, but in php this function is called 'ip2long'
Is ip2long() in PHP equal to INET_ATON() function in MySQL?
They are almost exactly the same. ip2long sometimes returns a negative value because PHP uses signed numbers for valuation, while MySQL uses unsigned.
Both are evaluated as x*(2^24) + y*(2^16) + z*(2^8) + w*(2^0)
, but in PHP, due to the long being signed, will show negative values for certain IP addresses.
For signed long, the range is
(2^31) - 1 = −2,147,483,648 to +2,147,483,647
So, addresses while translate to over +2,147,483,647 will wrap around and give negative values.
ip2long("254.254.254.254"); // -16843010
This link describes this in detail.
mysql where inet_aton like %...%
You could use the reverse function INET_NTOA
and you have only partial match ( '31.165.84') (missing the last digit)
SELECT * FROM table WHERE INET_NTOA(ip) = '31.165.84.4'
Related Topics
Which Is Fastest in PHP- MySQL or MySQLi
How to Prevent JavaScript Code Theft
How to Capture Search Term from Google Search
Rendering an Svg File to a Png or Jpeg in PHP
Phpexcel Download Using Ajax Call
Randomly Losing Session Variables Only in Google Chrome & Url Rewriting
Query MySQL and Export Data as CSV in PHP
Allow Users to Download Files Outside Webroot
Multi-Dimensional Array Post from Form
How to Allow Remote Access to My Wamp Server for Mobile(Android)
Accented Characters in MySQL Table
Subtracting a Certain Number of Hours, Days, Months or Years from Date
PHP Password_Hash and Password_Verify Issues No Match
Replace Add to Cart Button with a Read More Linked to Product Page on Shop Pages in Woocommerce 3