phpBeginner06 Posted January 20, 2007 Share Posted January 20, 2007 I am trying to create a unique hit counter with the script below (that I found on another website). I can get the script to set the cookie, but it will not send mysql_query UPDATE to the "ur_hits" data table; if the cookie does not exist (like the script should be doing).[u]Data Table[/u][code] CREATE TABLE ur_hits ( page VARCHAR(30) NOT NULL, ref VARCHAR(40) NOT NULL, user_id INT UNSIGNED, counter INT UNSIGNED, PRIMARY KEY (page, user_id) );[/code][u]PHP Hit Counter & HTML Code[/u][code]<?$db=mysql_connect("localhost","username","password");mysql_select_db("Statistics",$db);if (!isset($user_id)) { $user_id=get_new_user(); setcookie ("user_id", $user_id, time()+315360000, "", "http://www.mydomain.com, 0); }record_hit($user_id, $PHP_SELF); mysql_query("UPDATE ur_hits SET counter=counter+1 WHERE page = '$PHP_SELF' AND user_id=\"$user_id\" ",$db); ?> <html><head> <title>Untitled</title></head><body></body></html>[/code]Does any one know what I am doing wrong? If so, how can I fix it and/or get this unique hit counter to work? Quote Link to comment Share on other sites More sharing options...
matto Posted January 20, 2007 Share Posted January 20, 2007 Are register_globals set to off in your PHP configuration ?Try this script to see:[code]<?phpinfo();?>[/code] :) Quote Link to comment Share on other sites More sharing options...
phpBeginner06 Posted January 20, 2007 Author Share Posted January 20, 2007 The script you provide did not give any type of results; it was just a blank page. Which PHP Configuration file do I need to open too make sure register_globals set to "on"? If I set it to "on" will my script work?PS: I have MySQL 4.1.14 ruuning on my server; will the script I am using work with this version? Do I need a updated version of MySQL for my script to work; could that be the problem? Quote Link to comment Share on other sites More sharing options...
phpBeginner06 Posted January 20, 2007 Author Share Posted January 20, 2007 I just used this script:[code]<?phpInfo;?>[/code]and I found that the register_globals is "on".So does anyone have any idea why script will not work; since register_globals is set to "on"? Quote Link to comment Share on other sites More sharing options...
Crimpage Posted January 20, 2007 Share Posted January 20, 2007 Look at www.php.net/mysql for info on how to return error codes.You could to then,$query = mysql_query("UPDATE ur_hits SET counter=counter+1 WHERE page = '$PHP_SELF' AND user_id=\"$user_id\" ",$db);if (!$query){ // return the mysql error}Also, just put echo "Testing"; just above the mysql_query line, that will let you know that the script is making it to that section...Cheers,Dave Quote Link to comment Share on other sites More sharing options...
phpBeginner06 Posted January 21, 2007 Author Share Posted January 21, 2007 It must not be making it to that section; because it will not echo anything before mysql_query. Why would it not be making it to this section; does anyone know?As a reference to this script that I found; here is the link below. Maybe I just am not coding it right; like the script shows how too, but it seems like I am (I don't know).[url=http://trkwebhosting.com/modules/free-php-hit-counter-script.php]http://trkwebhosting.com/modules/free-php-hit-counter-script.php[/url] Quote Link to comment Share on other sites More sharing options...
matto Posted January 21, 2007 Share Posted January 21, 2007 Where is this funciton ? [code]get_new_user()[/code]it's not appearing anywhere in your code...and sorry the original code I gave you should have been [code]<?phpphpinfo();?>[/code] Quote Link to comment Share on other sites More sharing options...
SharkBait Posted January 21, 2007 Share Posted January 21, 2007 When I do MySQL queries I always have them in the form:[code]<?php$query = mysql_query($strqry) or die("MySQL Error: <br />{$strqry}<br /> ". msyql_error());?>[/code]That way if I have a mysql error, the query will die, it will spit out my actually query so I can see what it looks like and then the [code=php:0]mysql_error()[/code] tells me what is wrong with it. Quote Link to comment Share on other sites More sharing options...
phpBeginner06 Posted January 21, 2007 Author Share Posted January 21, 2007 Matto, Its in there; look at the code again - it is registered in a variable and the variable in the script is below.[code]"$user_id=get_new_user();"[/code] Quote Link to comment Share on other sites More sharing options...
matto Posted January 22, 2007 Share Posted January 22, 2007 what I meant is, where is the body of the get_new_user() function...eg:[code]<?phpfunction get_new_user() { //body of function goes here.....}?>[/code] Quote Link to comment Share on other sites More sharing options...
phpBeginner06 Posted January 23, 2007 Author Share Posted January 23, 2007 That is the problem. I do not know code I need to use for the get_new_user() function or for the record_hit() function. any ideas....... Quote Link to comment Share on other sites More sharing options...
matto Posted January 23, 2007 Share Posted January 23, 2007 Well.....The functions get_new_user() and record_hit() are not PHP specific functions but are user-defined functions, and these two functions are not included anywhere in your code - therefore you update query is failing.try putting this:[code]echo $user_id;exit;[/code]after this:[code]$user_id=get_new_user();[/code]....and see what it outputs (I think you will find it will print nothing at all).Looking a bit closer at you code I would assume the get_new_user() function (if it was present in your code) is meant to look up a database table and obtain the id for the user.....You may want to check out the site you got the code from in the first place..... :) Quote Link to comment Share on other sites More sharing options...
phpBeginner06 Posted January 23, 2007 Author Share Posted January 23, 2007 matto, i figured it out, but i went a different route - but [u][b][color=red]!!! THANK YOU !!![/color][/b][/u] for all your help.Below is the link to the final working script:[url=http://www.phpfreaks.com/forums/index.php/topic,123428.0.html#msg511224]http://www.phpfreaks.com/forums/index.php/topic,123428.0.html#msg511224[/url] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.