I am trying to mess with different ways to do a hit counter. I currently have two files. Hit Counter and Count visits. Count visits calls hit counter and hit counter creates a table and increments it. I am not getting any output and I think it is in my create table or somewhere around there. This is my hit counter file

class HitCounter {
       private $DBConnect;
private $DBName = "ngarrett_MIS4530A";
private $TableName = "4530-p5-iabarker"; private $Hits = 0;
function __construct() {
$TableName = "4530-p5-iabarker";
$DBName = "ngarrett_MIS4530A";
$this->DBConnect = @new mysqli("localhost", "**********", "**********"); if (mysqli_connect_errno())
die("<p>Unable to connect to the database server.</p>" . "<p>Error code " . mysqli_connect_errno()
. ": " . mysqli_connect_error()) . "</p>";
// The following code creates a table if one does not exist $SQLstring = "USE $DBName";
$QueryRsult = @mysqli_query($this->DBConnect, $SQLstring); $SQLstring = "SELECT * FROM $4530-p5-iabarker";
// See if table exists
$QueryResult = @mysqli_query($this->DBConnect, $SQLstring);

mysql_query("CREATE TABLE countertable( count INT(8))")or die("Create table Error: ".mysql_error());
       }
       function __destruct() {
               // Closes the database connection
               $this->DBConnect->close();
}
public function setDatabase($Database) {
$this->DBName = $Database; @$this->DBConnect->select_db($this->DBName)
Or die("<p>Unable to select the database.</p>"
. "<p>Error code " . mysqli_errno($this->DBConnect) . ": " . mysqli_error($this->DBConnect)) . "</p>";
}
public function setTable($4530-p5-iabarker) {
               $this->TableName = $4530-p5-iabarker;
       }
       public function setHits() {
//code to increment the hit counter and save the result in the table
       }
       public function getHits() {
}

and here is my count visits file that calls the hit counter

<?php

require_once("HitCounter.php");

?>


<h1>Hit Counter</h1>

<?php

echo "<p>There are _______ hits</p>";

$Database = "ngarrett_MIS4530A"; $Table = "4530-p5-iabarker";
if (class_exists("HitCounter")) {
$Counter = new HitCounter(); $Counter->setDatabase($Database);
} else
exit("<p>The HitCounter class is not available!</p>"); $Counter->setTable($4530-p5-iabarker);
$Counter->getHits();
$Counter->setHits();

//code to display counter goes here
mysql_query("UPDATE 4530-p5-iabarker SET count=count+1"); 

?>

My My, thats complex. I do all of that with 3 lines, having previously created the db.

<?php
include('php/config.php');
include('php/connect.php');

mysql_query("UPDATE admin SET hits=hits+1");

?>

Works for me!

The end of Line 11 has $SQLstring = "USE $DBName"; -- this should be bumped down to a new line, as it stands it is commented out.
I don't see the point of setting the database or table in Count Visits, if you already create a table in ngarrett_MIS4530A through your constructor.
My last tip is that you can use CREATE TABLE IF NOT EXISTS rather than having the whole script die if the table is already there in the DB. http://dev.mysql.com/doc/refman/5.1/en/create-table.html

And you keep calling this variable

$4530-p5-iabarker

But in the class you have set

private $TableName = "4530-p5-iabarker";

you mean in lines 12 and 28 right? where I call the 4530?

this is one of the errors that I am getting....

[31-Mar-2013 14:54:28] PHP Parse error: syntax error, unexpected '$', expecting '&' or T_VARIABLE in /home2/ngarrett/public_html/4530iabarker/proj5/HitCounter.php on line 38

would that be because it should be named TableName?

and also, any ideas on these functions?

public function setTable($TableName) {
               $this->TableName = $TableName;
       }
       public function setHits() {


//code to increment the hit counter and save the result in the table
       }
       public function getHits() {
}

?>

I agree with TonyG_cyprus as your code is too complex and have lots of issues. This code can be easily implemented within 3 lines as mentioned by TonyG_cyprus.

this is one of the errors that I am getting....

Hard to tell what the error is, as it is occurring in HitCounter.php on line 38, and you've only shown us until line 34.

and also, any ideas on these functions?

As stated earlier, I don't see a point to setTable(), since you already do so in the class constructor: private $TableName = "4530-p5-iabarker";
The only reason you would use this is if you needed to change your the table you are using, and I'm not sure why you would in this case.

For setHits() and getHits(), these functions should do as they are called. The former should run a SQL query increasing your count field by 1. The latter should run a SQL query retrieving the value of count.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.