insert ip address in mysql php code example

Example 1: mysql ip address data type

-- `ip_address` int(4) unsigned NOT NULL
INSERT INTO my_table (`ip_address`) VALUES (INET_ATON("127.0.0.1"));
SELECT INET_NTOA(ip_address) as ip FROM my_table;
-- php
<?php
var_dump(ip2long('123.63.153.253'));	-- 2067765757
var_dump(long2ip(2067765757));			-- "123.63.153.253"
?>

Example 2: how to insert ip address in mysql using php

-- `ip_address` int(4) unsigned NOT NULL
INSERT INTO my_table (`ip_address`) VALUES (INET_ATON("127.0.0.1"));
SELECT INET_NTOA(ip_address) as ip FROM my_table;
-- php
<?php
$ipaddress = $_SERVER['REMOTE_ADDR'];  
var_dump(ip2long('123.63.153.253'));	-- 2067765757
var_dump(long2ip(2067765757));			-- "123.63.153.253"
?>

Tags:

Sql Example