0

I have ip_addresses that were imported as strings into the database and stored as unsigned integers. I want to display them in standard xxx.xxx.xxx.xxx format in a table. In my code, I tried the following:

   <?php
  $query = $con->query('SELECT inet_ntoa(IP_ADDRESS)as address, FILENAME, country, area, city FROM download WHERE FILENAME is not null  ORDER BY country,area,city');                 

    while ($row = $query->fetch()) 
    {
         echo "<tr>";
        echo "<td>" . $row['$address'] ."</td>";
        echo "<td>" . $row['FILENAME'] ."</td>";
        echo "<td>" . $row['country'] . "</td>";
        echo "<td>" . $row['area'] . "</td>";
        echo "<td>" . $row['city'] . "</td>";
        echo "</tr>";
    }
    ?>

It no longer throws an error, but the ip's are all wrong, many starting with 0.0.0.

How do I do the conversion so I can display the ip_address correctly?

Thanks in advance,

Larry

Larry78723
  • 31
  • 6
  • This code, by itself, doesn't produce the syntax error described. Is it part of a larger block, which _does_ reproduce that error? If so, please show us the minimum amount of code needed for the error to occur. P.S. It might produce one saying the `inet_ntoa` function is undefined though, unless you've created such a function yourself in your script? I don't know if [this](https://stackoverflow.com/questions/2754340/inet-aton-and-inet-ntoa-in-php) will be useful in respect of creating such an implementation – ADyson May 04 '20 at 16:53
  • @ADyson, the ip's were originally strings in a .sql file and were imported to an unsigned int column without the use of 'inet_aton'. I've tried using inet_ntop and it also errors. I edited the code to include more of the code involved, – Larry78723 May 04 '20 at 17:18
  • Sorry I don't see any edits, did you definitely submit one? – ADyson May 04 '20 at 17:24
  • Check now edits posted – Larry78723 May 04 '20 at 17:26
  • Thanks. Well `| | \/` would definitely cause a PHP syntax error. Is that really part of the code? – ADyson May 04 '20 at 17:30
  • Like I said, you need to show enough of the real code to actually get it to generate the error you've pasted into your question. Because, aside from the `| | \/` which I've ignored, the code you've posted is valid syntax. Demo: http://sandbox.onlinephpfunctions.com/code/c3179257e3f2b7fcc90953b41271befd4f2ab9a4 . (Of course the demo generates other errors due to missing variables, but those are run-time errors, which proves that the code compiles and runs, whereas a syntax error is a compile-time error and means the code never executes at all). – ADyson May 04 '20 at 17:32
  • If you really do have a syntax error, it's quite likely to be originating from somewhere else in your script - unfortunately PHP is not always that good at telling you where the root cause of the problem is. It can tell you where the parser failed to interpret an instruction, but very often the real reason for that is actually somewhere else. You may have to go through it with a fine toothcomb to find it. – ADyson May 04 '20 at 17:34
  • @ADyson The code now runs without error as posted above, but the ip's are all wrong. I believe the problem is that they were xxx.xxx.xxx.xxx strings that were imported through phpMysql into a column that is formatted as int(50) unsigned. – Larry78723 May 04 '20 at 18:20
  • 1
    Ok. You'll need to show some examples of the values that are being stored, what result your query produces when you try to convert them, and what result you expected (if you know). Do you know _exactly_ how they were imported? i.e. do you still have the import file, and the script which was run to import them? – ADyson May 04 '20 at 18:23

0 Answers0