Categories
MySQL

Store IP Addresses as UNSIGNED INT

Want to store IP addresses in database and still using VARCHAR(15)? You can actually store IP addresses as integer values because using INT you go down to 4 bytes of space and have a fixed size field.

You may like:  Primary Key, Unique Key, Key, Foreign Key

As the range of a IP Addresses are 32 bit unsigned integer you can use your column as UNSIGNED INT.

In your MySql query you can use INET_ATON() to convert and IP to an integer, and INET_NTOA() for vice versa.

In PHP, ip2long() and long2ip() are same as above functions.

Example to store IP Addresses:

$sql = “UPDATE tbl SET ip = INET_ATON(‘{$_SERVER[‘REMOTE_ADDR’]}’) WHERE `tbl_column` = ‘$id'”;

Avatar for Namaste UI (Author)

By Namaste UI (Author)

Namaste UI collaborates closely with clients to develop tailored guest posting strategies that align with their unique goals and target audiences. Their commitment to delivering high-quality, niche-specific content ensures that each guest post not only meets but exceeds the expectations of both clients and the hosting platforms. Connect with us on social media for the latest updates on guest posting trends, outreach strategies, and digital marketing tips. For any types of guest posting services, contact us on info[at]namasteui.com.

Leave a Reply

Your email address will not be published. Required fields are marked *