...

View Full Version : php mysql query



hmvrulz
02-23-2008, 04:46 AM
am getting an error
Unknown column '$code' in 'where clause'


<?php

$server = 'localhost'; // MySQL hostname
$username = 'root'; // MySQL username
$password = ''; // MySQL password
$dbname = 'ip2l'; // MySQL db name

// connecting to database
$db = mysql_connect($server, $username, $password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$ip = $_SERVER['REMOTE_ADDR'];
$ip = "59.96.40.247"; // assigned dummy ip to test the script localy
echo $ip;
echo '<br/>';
$numbers = preg_split( "/\./", $ip);
$code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);
echo $code;
echo '<br/>';

$sql = 'SELECT * FROM `ipcountryregioncitylatitudelongitudeispdomain` WHERE ipFROM < $code && ipTO > $code';
$get_info_result = mysql_query($sql, $db) or die(mysql_error());
$info = mysql_fetch_array($get_info_result);

$country_code = $info[countrySHORT];
$country_name = $info[countryLONG];
$region = $info[ipREGION];
$city = $info[ipCITY];
$latitude = $info[ipLATITUDE];
$longitude = $info[ipLONGITUDE];
$isp = $info[ipISP];
$domain = $info[ipDOMAIN];
echo $country_name;
?>

am getting the error from this code can u plz correct it
its a ip to geo based locator...
where the code is the converted ip number which should be between the rage of ipFROM and ipTO


$sql = 'SELECT * FROM `ipcountryregioncitylatitudelongitudeispdomain` WHERE ipFROM < $code && ipTO > $code';

nissar
02-23-2008, 04:56 AM
$sql = 'SELECT * FROM `ipcountryregioncitylatitudelongitudeispdomain` WHERE ipFROM < $code && ipTO > $code';

change to


$sql = 'SELECT * FROM `ipcountryregioncitylatitudelongitudeispdomain` WHERE ipFROM < $code AND ipTO > $code';

cheers

hmvrulz
02-23-2008, 05:25 AM
$sql = 'SELECT * FROM `ipcountryregioncitylatitudelongitudeispdomain` WHERE ipFROM < $code AND ipTO > $code';
on changing to the above

Unknown column '$code' in 'where clause'
am getting the same error

abduraooft
02-23-2008, 06:53 AM
If you print your query (echo $sql;) you may see the portion ipFROM < $code remains as such, which is causing the error. You have to pass the value of $code. So Chage it like


$sql = "SELECT * FROM `ipcountryregioncitylatitudelongitudeispdomain` WHERE ipFROM < '$code' && ipTO > '$code' ";

PS: reaally surprised to see you table name ;)

hmvrulz
02-23-2008, 11:24 AM
If you print your query (echo $sql;) you may see the portion ipFROM < $code remains as such, which is causing the error. You have to pass the value of $code. So Chage it like


$sql = "SELECT * FROM `ipcountryregioncitylatitudelongitudeispdomain` WHERE ipFROM < '$code' && ipTO > '$code' ";

PS: reaally surprised to see you table name ;)


$sql = "SELECT * FROM ipcountryregioncitylatitudelongitudeispdomain WHERE ipTO >=$code AND ipFROM <=$code";



i ddint notice.... for $sql = ' '; is wht i used n it should be $sql = " "; since am passing variables..

thanx abduraooft

the table name is so since i just imported wht i got off the net... not my creation...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum