0

Hello I want to save ip_addresses into my mysql database. It should work for ipv4 and ipv6. I found the solution to use inet_pton from php. However 90% of the entries are fine but 10% fails to convert with inet_nton back to ipv6 or ipv4 (not sure which one)

my database looks like so

ip - varchar(30) - latin1_bin

I just do

$ip_pton = inet_pton($_SERVER["REMOTE_ADDR"]);

if I try to convert back I just use inet_ntop. Like above I said maybe from 100 entries I get just few warnings

Warning: inet_ntop(): Invalid in_addr value

Example converted ip is this "`E" It cannot be converted back. I get the error above.

I did I maybe use wrong data types in mysql?

MaZy
  • 81
  • 1
  • 11
  • Check this. Possible Answer for your question. https://stackoverflow.com/questions/5133580/which-mysql-datatype-to-use-for-an-ip-address/5133610#5133610 – Umashankar Das Jun 03 '17 at 16:29
  • @chris85 but I don't save real ipv4 & ipv6. I convert it first. You see the quoted example above? This cannot be over 30 chars :p. Maybe I just use full length and do something like varchar(50) and I put ipv6 and ipv4 there. – MaZy Jun 03 '17 at 16:45
  • Oh, yup, you are right. Can you post a valid an example and an erring example? – chris85 Jun 03 '17 at 17:01

0 Answers0