The PHP Group PHP Logo
Posted on Mar 19, 2009

Php counter im trying to add a visitor counter to my homepage i downloaded the script but not sure what to do and where to put things to get it running this is the counter script... // Connect to server and select database. mysql_connect(''$host'', ''$username'', ''$password'')or die(''cannot connect to server ''); mysql_select_db(''$db_name'')or die(''cannot select DB'') $tbl_name=''counter''; // Table name $sql=''SELECT * FROM $tbl_name''; $result=mysql_query($sql); $rows=mysql_fetch_array($result); $counter=$rows['counter']; // if have no counter value set counter = 1 if(empty($counter)){ $counter=1; $sql1=''INSERT INTO $tbl_name(counter) VALUES('$counter')''; $result1=mysql_query($sql1); } echo ''You 're visitors No.'' echo $counter; // count more value $addcounter=$counter+1; $sql2=''update $tbl_name set counter='$addcounter'''; $result2=mysql_query($sql2); mysql_close(); ?> and this is mode homepage code.. LOGIN Wanna find out all the latest upto date infomation on the AbZoRbeD network, Then check this out. Dont idle for no reason join in the game and have a little fun battling against the other players. Use this forum for discussing all kinds of topics you must be registered and logged in to access this area if you would like to register Click Here Contact Us © AbZoRbeD.net 2007-2009 any help would be grate :)

  • 1 more comment 
  • d_howley Mar 19, 2009

    sorry i forgot to say that i have that all setup

    just didnt want my username and password getting out :)

  • d_howley Mar 20, 2009

    should the path to the counter.php go in here i got the counter.php in a folder called counter because i keep getting the error ... PHP Fatal error: Call to undefined function counter()

  • d_howley Mar 20, 2009

    i did what you said now im just getting an error .... Fatal error: Call to undefined function counter()

×

2 Answers

Anonymous

Level 1:

An expert who has achieved level 1.

Mayor:

An expert whose answer got voted for 2 times.

  • Contributor 4 Answers
  • Posted on May 09, 2009
Anonymous
Contributor
Level 1:

An expert who has achieved level 1.

Mayor:

An expert whose answer got voted for 2 times.

Joined: May 09, 2009
Answers
4
Questions
0
Helped
14654
Points
9

You might want to note that this is not the most effective manner of creating a counter in PHP. On a few of my websites, I find it is much more effective to simply create a text file, eg. counter.txt and have PHP read that every time the page is loaded, instead of running a MySQL query. Then, have PHP also update the counter.txt file by adding 1 to the number already listed in the file and read to the user.

Dubravko

Level 1:

An expert who has achieved level 1.

Corporal:

An expert that has over 10 points.

Problem Solver:

An expert who has answered 5 questions.

  • Contributor 10 Answers
  • Posted on Mar 19, 2009
Dubravko
Contributor
Level 1:

An expert who has achieved level 1.

Corporal:

An expert that has over 10 points.

Problem Solver:

An expert who has answered 5 questions.

Joined: Mar 19, 2009
Answers
10
Questions
0
Helped
15776
Points
12

These lines

mysql_connect(''$host'', ''$username'', ''$password'')or die(''cannot connect to server '');
mysql_select_db(''$db_name'')or die(''cannot select DB'')
$tbl_name=''counter''; // Table name
$sql=''SELECT * FROM $tbl_name'';

say that you need mysql database. user name, password and table in that database called counter....

So if you do please change parameters if you don't, dont yuse this script, use just some site with free simple counter for your site, which is maintained on their site

  • Dubravko Mar 19, 2009

    Then, the best way is to wrap it into some function, include that code in homepage and call function from somewhere in page where you want it to be seen , for ex.


    function counter()
    {
    //copy counter code here
    }
    ?>.....



    ....




  • Dubravko Mar 20, 2009

    See this page for includes, don't change function call.

    http://www.w3schools.com/PHP/php_include...

    in your case




×

Ad

Add Your Answer

×

Uploading: 0%

my-video-file.mp4

Complete. Click "Add" to insert your video. Add

×

Loading...
Loading...

13,428 views

Ask a Question

Usually answered in minutes!

Top The PHP Group Computers & Internet Experts

Rob Hill
Rob Hill

Level 3 Expert

1483 Answers

Nikole Brown
Nikole Brown

Level 2 Expert

124 Answers

Syed Bilal
Syed Bilal

Level 3 Expert

252 Answers

Are you a The PHP Group Computer and Internet Expert? Answer questions, earn points and help others

Answer questions

Manuals & User Guides

Loading...