^^
PHP Code:
<?php
mysql_connect('localhost',"$username","$password");
mysql_select_db('hitcounter');
/*Lets update the table named 'counter' that holds the count in a field name 'count'. The field 'count' is of INT type!!*/
mysql_query('UPDATE hitcounter SET count=count+1');
/*Lets fetch the updated value of the table*/
$resource=mysql_query('SELECT count FROM hitcounter');
$count=mysql_result($resource,0,'count');
echo 'This page has served paged '.$count.' times.';
?>
Also, if you want to have to store separate counts for each webpage, then we will have to
the 'ID' field that should be linked to the web page name or path. The way of calling the counter functions also would need to be modified.
Since you have asked for suggestions:
1. in PHP, understand when you should use double quotes and when single quotes. A string should be enclosed in double quotes when it contains a variable that should be parsed while outputting it. Enclosing string in double quotes means that PHP has to do extra work to search if there is any variable in the string. If its there, fine, else waste of time and energy for the parser. So if your string contains no variable, better to enclose it within single quotes.
2. In a MySQL table, have the appropriate database fields. There is no need to have 2 fields. Once will suffice. Also, since we need mathematical manipulations, we should SET the type of field to 'INT'. This way, we won't have to use PHP's math ability to update the count.
3. Since we are selecting only one column from the table, we can use 'mysql_result()' rather than 'mysql_fetch_*()' functions.
4. The ' or die(mysql_error())' tip: You already know it. So no need stressing it.