Jump to content

[SOLVED] PHP/MYSQL - Simple hit counter help


djcochran

Recommended Posts

I'm trying to use read data from a table and increment the visitor counter on my site, but I'm having a little trouble. My mysql table is like this (data is int(11))...

 

-counters-

count - 0

cnum - 10000

onum - 1000

 

I'm trying to increment count each time someone visits the site. I've tried several different approaches that I've found online, but I can't get anything to work.

 

<?php
// Connects to your Database
mysql_connect("localhost", "xxxxx", "xxxx") or die(mysql_error());
mysql_select_db("student5") or die(mysql_error());

//Adds one to the counter
mysql_query("UPDATE counters SET count = count + 1");

//Retreives the current count
$count = mysql_fetch_row(mysql_query("SELECT count FROM counters"));

//Displays the count on your site
print "$count[0]";
?> 

 

This gives me the error

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /export/home/student5/html/countprac.php on line 10.

 

I've tried several different codes and got similar results. Any tips?  ???

Link to comment
Share on other sites


$count = mysql_fetch_row(mysql_query("SELECT count FROM counters"));

 

change the above line to...

 


$query = mysql_query("SELECT count FROM counters");
$count = mysql_fetch_row($query);

 

let us know if htat helps...

Link to comment
Share on other sites

Your mysql_query() statements don't have any error checking, so no one can really tell you why they are failing. And don't nest function calls - mysql_fetch_row(mysql_query("SELECT count FROM counters")) because it makes it impossible to do any error checking, error reporting, or error recovery in your code.

 

Change your first query to this -

 

mysql_query("UPDATE counters SET count = count + 1") or die("Update query failed: " . mysql_error());

 

Change your second query to this -

 

$query = "SELECT count FROM counters";

$result = mysql_query($query) or die("Select query failed: " . mysql_error());

$count = mysql_fetch_row($result);

Link to comment
Share on other sites

Waaaait...I'm looking at it right now and it lists it kind of like this...

 

name | num

---------------

count - 0

cnum - 10000

onum - 1000

 

Is name the column I'm looking for? Or num (since it's the data)? I just thought use and select were to select the table and the specific row (like count and cnum).

Link to comment
Share on other sites

Yes and wut exacly are you trying to do.

MySQL needs to know which record you are refferring to and how.

 

but if your trying to grab all records into an array. than you will have to loop

$count = mysql_fetch_row(mysql_query("SELECT num FROM counters WHERE name='count'"));

 

so you have to follow what yer field names are

 

here we just tell it, we want field 'num' from counters WHERE name is 'count'

 

Link to comment
Share on other sites

I was making a statement, yer SQL statement was wrong.

as it was referencing the contents of the fields, instead of referencing the fields themselves.

 

name | num

---------------

count - 0

cnum - 10000

onum - 1000

 

The field names are the headers in this table. Not the contents.

 

look at the sql I provided above. That shud do wut u want.

 

Link to comment
Share on other sites

Well...this is what my code looks like now, but I'm still getting the same "Update query failed: Unknown column 'count' in 'field list'."  :(

 

<?php
// Connects to your Database
mysql_connect("localhost", "xxxx", "xxxx") or die(mysql_error());
mysql_select_db("student5") or die(mysql_error());

//Adds one to the counter
mysql_query("UPDATE counters SET count = count + 1") or die("Update query failed: " . mysql_error());

//Retreives the current count
$query = "SELECT count FROM counters";
$result = mysql_query($query) or die("Select query failed: " . mysql_error());
$count = mysql_fetch_row(mysql_query("SELECT num FROM counters WHERE name='count'"));

//Displays the count on your site
print "$count[0]";
?> 

Link to comment
Share on other sites

mysql_query("UPDATE counters SET num = num + 1 WHERE name='count'")

look at the previous queries they all reference the content again, not the field names.

 

you have 2 field names, name and num.

 

not count, onum, cnum... these are the contents of the field name

 

so u have to tell MySQL what field yer updating and which row.

The field yer updating is 'num'

where the row has field 'name' contains 'count'

 

Again yer table has no 'count' field, this is where yer misunderstanding

 

go through yer code again, and look if you can fix the rest :) its easy after ya get the hang of it

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.