0

I "inherited" from a collegue a MySQL database with dozen of fields. Since I've worked very few times with databases, I'm looking for little advice here.

There's a binary(16) field named "IP": I suppose it is used to store user IPs. A typical stored value is, for example, 00000000000000000000ffff3d024463.

Using PHP (or even MySQL, if this is possibile), how can I convert this data to a plain IP address?

2 Answers 2

0

have you tried

$data = hex2bin($ip);
var_dump($data);
//not sure how your data was put into the column IP

on your sql query you can also try

$query = mysql_query("SELECT `HEX(IP)` FROM `database`");
$row = mysql_fetch_array($query);
foreach($row['ip'] as $ip)
{
   $data = hex2bin($ip);
   echo $data."<br />";
}

//code not tested

SQL Query with binary data (PHP and MySQL)

http://php.net/manual/en/function.hex2bin.php

http://php.net/manual/en/function.bin2hex.php

Sign up to request clarification or add additional context in comments.

4 Comments

The hex2bin returns bool(false)
Does this page help at all? I honestly would change the column from binary to varchar and store ip address as string. stackoverflow.com/questions/6382738/…
Try bin2hex ();
I must not alter the database, I only can retrieve values
0

Solved: I had to convert that number using a MySQL function (INET6_NTOA).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.