Re: Select statement drops 10th digit during table lookup?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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):-
<?php
	/*
		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."<BR>";
	echo "cn:".$data["cn"]."<BR>";
	echo "cc:".$data["cc"];

	dbclose($conn);
?>

HTH !!

Ronan
e: ronan@thelittledot.com
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


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux