Why not use the built-in conversion functions in mysql?
>From the manual:
INET_NTOA(expr)
Given a numeric network address (4 or 8 byte), returns the dotted-quad representation
of the address as a string:
mysql> SELECT INET_NTOA(3520061480);
-> "209.207.224.40"
INET_ATON(expr)
Given the dotted-quad representation of a network address as a string, returns an
integer that represents the numeric value of the address. Addresses may be 4 or 8 byte
addresses:
mysql> SELECT INET_ATON("209.207.224.40");
-> 3520061480
Doug
On Fri, 13 Jun 2003 10:19:32 +0100, Ronan Chilvers wrote:
>Here's how I did it when playing with this...
>
>Table definition:-
>
>CREATE TABLE ip_list (
> ip_from double default NULL,
> ip_to double default NULL,
> country_code char(2) default NULL,
> country_name varchar(100) default NULL,
> KEY ip_from (ip_from,ip_to,country_code,country_name)
>) TYPE=MyISAM;
>
>Basic code to query it (takes a parameter 'ip' as GET paramter):-
> /*
> IP Lookup script
> * prints a country code and name for an ip in the $_GET array
> */
>
> //Vars & Constants
> define("HOST","myhost");
> define("USER","myuser");
> define("PASS","mypassword");
> define("DB","ips");
> define("IP",$_GET["ip"]);
>
> // Functions
> function dbconnect() {
> if (($conn=mysql_pconnect(HOST,USER,PASS))===false) {
> die("Couldn't connect to server");
> }
> if (!mysql_select_db(DB,$conn)) {
> die("Couldn't select database");
> }
> return $conn;
> }
> function dbclose($conn) {
> if (!mysql_close($conn)) {
> die("Couldn't close database connection");
> }
> return true;
> }
> function dosql($SQL) {
> if (($result=mysql_query($SQL))===false) {
> die("Couldn't do sql : $SQL");
> }
> return $result;
> }
>
> //Core
> $SQL = "SELECT COUNTRY_NAME AS cn, COUNTRY_CODE AS cc FROM ips.ip_list WHERE
> IP_NUM BETWEEN ip_from AND ip_to";
>
> $conn = dbconnect();
>
> $ip = "127.0.0.1";
>
> $QSQL = str_replace("IP_NUM",sprintf("%u",ip2long(IP)),$SQL);
>
> $result = dosql($QSQL);
> $data = mysql_fetch_array($result);
>
> echo "ip:".IP."";
> echo "cn:".$data["cn"]."";
> echo "cc:".$data["cc"];
>
> dbclose($conn);
>?>
>
>HTH !!
>
>Ronan
>e: [EMAIL PROTECTED]
>t: 01903 739 997
>w: www.thelittledot.com
>
>The Little Dot is a partnership of
>Ronan Chilvers and Giles Webberley
>
>--
>PHP Database Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php