-
August 31st, 2002, 10:32 AM
#1
Hit Counter: DB Version
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 */
require_once('DB.php');
// 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 (DB::isError($db)) { die ($db->getMessage()); }
// Query the DB: $sql = "SELECT hitcount.count FROM hitcount"; $q = $db->query($sql); if (DB::isError($q)) { die ($q->getMessage()); }
// Get DB Results to array $count $count = $q->fetchRow();
// Set $counter to current value and increment $counter = $count[0]; $counter++; $oldcount = $count[0];
// Write the new count to the DB: $sql = "UPDATE hitcount SET count=$counter WHERE count=$oldcount"; $q = $db->query($sql); if ($db::isError($q)) { die ($q->getMessage()); }
// Write the counter to the screen: ?> <font size="5"><?= $counter ?></font>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|