larry29936 Posted May 4, 2020 Share Posted May 4, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/310695-convert-unsigned-int-ip_address-from-db-to-string/ Share on other sites More sharing options...
Barand Posted May 4, 2020 Share Posted May 4, 2020 Are you sure that is line 205? Quote Link to comment https://forums.phpfreaks.com/topic/310695-convert-unsigned-int-ip_address-from-db-to-string/#findComment-1577539 Share on other sites More sharing options...
larry29936 Posted May 4, 2020 Author Share Posted May 4, 2020 (edited) @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 May 4, 2020 by larry29936 Quote Link to comment https://forums.phpfreaks.com/topic/310695-convert-unsigned-int-ip_address-from-db-to-string/#findComment-1577542 Share on other sites More sharing options...
gw1500se Posted May 4, 2020 Share Posted May 4, 2020 (edited) inet_ntoa and inet_aton are MySQL functions. Use them in your queries. Edited May 4, 2020 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/310695-convert-unsigned-int-ip_address-from-db-to-string/#findComment-1577543 Share on other sites More sharing options...
Barand Posted May 4, 2020 Share Posted May 4, 2020 (edited) Were they originally saved using INET_ATON to convert from "w.x.y.z" to an integer? Edited May 4, 2020 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/310695-convert-unsigned-int-ip_address-from-db-to-string/#findComment-1577547 Share on other sites More sharing options...
larry29936 Posted May 4, 2020 Author Share Posted May 4, 2020 No, the file was imported thru phpMysql import function as it was. I'm going to clear the table and re-import in the mysql shell using inet_aton Quote Link to comment https://forums.phpfreaks.com/topic/310695-convert-unsigned-int-ip_address-from-db-to-string/#findComment-1577548 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.