...

View Full Version : What's wrong with my code



php_stud
05-17-2004, 08: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.




<?php

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

// Establishing a database connection


$db=mysql_connect("localhost","user","password");
mysql_select_db("hits",$db);

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


do{

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

mysql_select_db("country",$db);

// 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
$ccode_array=mysql_fetch_array($country_exec);

// getting the country name from the array
$country_name=$ccode_array['country_name'];

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

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


?>

aspdude2004
05-17-2004, 06: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:

firepages
05-17-2004, 09: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);

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

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

http://www.codingforums.com/postguide.htm

php_stud
05-20-2004, 08: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. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum