Question Overflow
Question Overflow

Reputation: 11255

How to read IPv6 address stored as a varbinary in MySQL?

I am storing IPv6 addresses as a varbinary with PHP function inet_pton($_SERVER['REMOTE_ADDR']). When I run an SQL query such as:

SELECT ip_address FROM some_table WHERE id='some_id';

I wouldn't be able to read the ip_address because it is in a binary format.

I notice that there is a corresponding MySQL function INET6_NTOA(expr) in MySQL version 5.6 to revert it back to readable format. Unfortunately, I am using MySQL version 5.5, which doesn't have that function.

Is there any other way I can read the IPv6 addresses without going back to PHP to do the conversion? I can easily read off the hexadecimal notation of the binary string with the editor in the Workbench as shown in the image attached, so I thought there should be an easy way to do this.

enter image description here

Upvotes: 2

Views: 2521

Answers (1)

Dmitry Reznik
Dmitry Reznik

Reputation: 6862

You can write user-defined functions to do the conversion for you and call them in the select clause. See this link for more info.

Upvotes: 3

Related Questions