My MySQL originally had IP addresses in varchar 'e, but on the Internet they write that it is best to use INT ( UNSIGNED ) to store IP addresses. I found information about certain functions like INNET_NTOA() , INNET_ATON() in MySQL, and in php ip2long() . So, I use the PHP ip2long() function, respectively, in the INT (11) UNSIGNED .

But now the IP has been converted to this: 396157915, that is, it returns numbers without dots. Actually the question is how to bring it into a normal view?

  • To convert from number to address - INET_NTOA - write to the database immediately - Boris Pobezhimov
  • "To convert from number to address - INET_NTOA - write to the database immediately" what exactly do you mean, please specify? - Webmaster
  • You do wrong. You do ip2long when writing, but you still need long2ip when reading. - VladD
  • Wrote your code, can you give an example, please? Something can not understand it all. - Webmaster

1 answer 1

execute this query select INET_ATON ('8.8.8.8'), INET_NTOA (134744072)

  • Here is an example of my code: $ mysqli-> query ("SELECT id FROM table WHERE ip = '". $ Ip. "'") -> fetch_row ()) {where $ ip am writing so $ ip = sprintf ('% u' , ip2long ($ ip)); - Webmaster
  • Honestly, I don't know php. if you execute the code in the mysql console that I gave above, you will see that it works. maybe you shouldn't bother with php functions but use mysql functionality directly - plesser
  • Well, then you can give a sample code on MySQLi, but not a request to the database through the console. It's just that I have not only one IP there, a bunch of them, actually from the $ ip ip change of users. - Webmaster
  • So I above and gave a sample code. See, you need to insert into the table - then, in theory, you write like this: $ mysqli-> query ("insert into table (ip, nameserver) values ​​(INET_ATON ('8.8.8.8'), ' site.com')" ) . Now you need a sample, then write something like this: $ mysqli-> query ("SELECT INET_NTOA (ip), nameserver FROM table '"). I note that it is not safe to transfer data directly to sql queries. - plesser
  • Solved the problem, as mentioned above, it turns out to be another long2ip to do when reading data. Thanks Vlad, and of course for your help. The only question is whether ip2long () is enough or just like that $ ip = sprintf ('% u', ip2long ($ ip)); tell me, I will be glad. :) - Webmaster