View Full Version : What's wrong with my code

05-17-2004, 09:09 AM
Well, im back to php and right now, just trying to do a custom stat for my web... i know that there are lots of logfile analyzers, but i'm just trying to make this one work... First, php executes this code very slow, second it dont insert the matching records and the country to the last database connection...

thanks for any possible assistance here.


// Sample code to display Visitor Country information
// PHP 4

// Establishing a database connection


$all_records = "SELECT * from views WHERE date_visit='2004-05-13'";
$recordset = mysql_query($all_records,$db);
$myrow = mysql_fetch_array($recordset);


$id = $myrow['id'];
$page = $myrow['page'];
$ip = $myrow['ip'];


// Query for getting visitor countrycode

$country_query = "SELECT country_name FROM iptoc WHERE IP_FROM<=inet_aton('$ip') AND IP_TO>=inet_aton('$ip') ";

// Executing above query
$country_exec = mysql_query($country_query);

// Fetching the record set into an array

// getting the country name from the array

//Insert the matching record to the database
$sql2="INSERT INTO views (id,page,date_visit,time_visit,ip,country) VALUES ('$id','$page','$date_visit','$time_visit','$ip','$country')";

} while ($myrow=mysql_fetch_array($recordset));


05-17-2004, 07:43 PM
firstly may i suggest you put your php in the php tags, secondly i think but im not sure that your database name has to be quoted .. you did it like so

$country_query = "SELECT country_name FROM iptoc WHERE IP_FROM<=inet_aton('$ip') AND IP_TO>=inet_aton('$ip') ";

where it should be

$country_query = "SELECT country_name FROM 'iptoc' WHERE IP_FROM<=inet_aton('$ip') AND IP_TO>=inet_aton('$ip') ";

but im not sure.... good luck :cool:

05-17-2004, 10:06 PM
best way to debug this stuff is to let MySQL help you , e.g.

$country_exec = mysql_query($country_query) or die($country_query . mysql_error() ) ;

that way you find out if your query even executed , if not you should get a reason why.

+ if you are using a resourse identifier you have to use use it.
$country_exec = mysql_query($country_query,$db);

05-20-2004, 02:34 AM
Thanks. I figured out what's wrong.

05-20-2004, 07:40 AM
Also you need to read our posting guidelines section 2


05-20-2004, 09:16 AM
Sorry, next time, i'll try to keep clear subjects...

well.. i was trying to change it... but it did not reflect in the thread listings... i did intentionaly try to reply in this thread bcoz i was navigating the site, and have seen that spookster seems to keep replying by "read our posting guidelines as you are required..."

it's good anyway, it reminded us to stay focus... and make this site worthed for those who are interested in web.

anyway, sorry again Sir. :)