Ok, this is the database enabled version of the hit counter I posted yesterday.
Obviously you'll need to change the values of variables at the top to be specific to your requirements, like change the $username and $password values, and possibly comment out $dbtype = "pgsql" and uncomment $dbtype = "mysql" depending on which one you use (I use PostgreSQL, which is why my code always assumes pgsql but provides a commented-out option for mysql support.

As you can see, it's a little more involved than the file-based version, but it'll work on servers where file-based operations are not permitted (i.e. most public web servers).

Again, this version hasn't been tested (I've installed Apache and PHP now, but I haven't got round to installing PostgreSQL ) so it may or may not work, but it gives you the general idea of how such a hit counter might work.

PHP Code:

Hit Counter: Database Version
Andrew J. Bennieston


// DB Connection Setup:
$username "hitcount";
$password "hitcount";
$host "localhost";
$database "phpstuff";

// Select DB Application:
// $dbtype = "mysql";
$dbtype "pgsql";

// Connect to the DB:
$dsn "$dbtype://$username:$password@$host/$database";
$db DB::connect($dsn);
if (
    die (

// Query the DB:
$sql "SELECT hitcount.count FROM hitcount";
$q $db->query($sql);
if (
    die (

// Get DB Results to array $count
$count $q->fetchRow();

// Set $counter to current value and increment
$counter $count[0];
$oldcount $count[0];

// Write the new count to the DB:
$sql "UPDATE hitcount SET count=$counter WHERE count=$oldcount";
$q $db->query($sql);
if (
    die (

// Write the counter to the screen:
<font size="5"><?= $counter ?></font>