PHP - HIT COUNTER USING MYSQL
Here's how to host your own hit counter using PHP and MySQL, including the exact time of counts.
Use
phpMyAdmin to create a new table in your MySQL database. Or if you are comfortable with PHP, write a script instead.
Open your phpMyAdmin then click on your database then on
SQL at the top.
In that box type:
PHP Code:
CREATE TABLE `counter` (
`id` int(10) NOT NULL auto_increment,
`date` varchar(225) NOT NULL default '',
PRIMARY KEY (`id`)
);
Now that the table is all set up, you need to add some script to your page.
Open up your homepage and in your
head tags enter this code:
PHP Code:
<?
$connect = mysql_connect(HOSTNAME,USERNAME,PASSWORD);
if (!$connect) { echo("ERROR: " . mysql_error() . "\n"); }
mysql_select_db("DATABASE NAME", $connect);
$date = date("l, j.n.Y g:i a");
$SQL = " INSERT INTO counter ";
$SQL = $SQL . " (date) VALUES ";
$SQL = $SQL . " ('$date') ";
$result = mysql_db_query(DATABASENAME,"$SQL",$connect);
if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); }
?>
This snippet goes where you want the counter to appear on your page:
PHP Code:
<?
$db = "DATABASE NAME";
$connect = mysql_connect(HOSTNAME,USERNAME,PASSWORD);
if (!$connect) { echo("ERROR: " . mysql_error() . "\n"); }
mysql_select_db($db);
$SQL = "SELECT * FROM counter ORDER BY id DESC LIMIT 1";
$retid = mysql_db_query($db, $SQL, $connect);
while ($row = mysql_fetch_array($retid)) {
$id = $row["id"];
print("$id");
}
?>