Jump to content

convert unsigned int ip_address from db to string


larry29936

Recommended Posts

I have ip_addresses stored in the database 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
while ($row = $query->fetch()) 
{
    $address = inet_ntoa($row['IP_ADDRESS']);  //Throws error here
    echo "<tr>";
//    echo "<td>" . $row['IP_ADDRESS'] ."</td>";
    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 throws the following error:

Parse error: syntax error, unexpected 'inet_ntoa' (T_STRING) in /home/larry/web/test/public_html/report1.php on line 205

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

Thanks in advance,

Larry

 

Link to comment
Share on other sites

@Barand, I fixed the code so it doesn't error, but the ip's are wrong, many displaying as 0.0.123.678, which I know are wrong. The new code is:

<?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>";
    }
?>

The sql file that the data was imported from had the ip's as strings that went into a column that is int(50) unsigned. I checked the import file and none of the imported ip's start with 0.0.

Edited by larry29936
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.