I have seen a few different ways to solve this problem, but no solution has worked for me. I am logging into a database and incrementing a hit counter when someone goes to the page. I have two files, hitcounter.php and count visits.php Count visits is used by the visitor and is needed to instantiate the hitcounter.php file. The hit counter should open the database and store the hits in a private variable. I have logged in to the database, incremented everything. I just dont know how to get it to count correctly and if I a using a private variable my code is below....

    $user_name = "";
    $password = "";
    $database = "";
    $TableName = "";

    $DBConnect = @mysqli_connect("", "", "", $)
            Or die("<p>Unable to connect to the database server.</p>"
            . "<p>Error code " . mysqli_connect_errno()
            . ": " . mysqli_connect_error()) . "</p>";
        $DBName = "";

    if(! $DBConnect )
    {
      die('Could not connect: ' . mysql_error());
    }

    $page = $_SERVER['REQUEST_URI'];
        $result = mysql_query("SELECT HIT_COUNT FROM  Hit_Count WHERE Page_Name = '$page'");


    $any = false;
    while($row = mysql_fetch_array($result)) {
        $any = true;
        $counter = mysql_result($result, 0) + 1;
        $sql = "UPDATE Hit_Count SET HIT_COUNT = '$counter' WHERE Page_Name = '$page'";
        mysql_query($sql);
        echo($counter);
    }

    if($any == false) {
        $sql = "INSERT INTO Hit_Count (Page_Name, HIT_COUNT) VALUES ('$page', '1')";
        mysql_query($sql);
        echo("1");
    }


    mysql_close($DBConnect);

    ?>`

I'm not expert, but you're opening the db with mysqli and your queries are mysql,
There's a starting point for debug.
Is this both files combined? you said you had 2
Are you getting any errors?

Member Avatar for LastMitch

I have two files, hitcounter.php and count visits.php Count visits is used by the visitor and is needed to instantiate the hitcounter.php file.

Even before anyone can help you with this.

Why is your code all green?

It shouldn't be green at all.

Like what TonyG mention why are you using mysqli connection when you are using mysql as a query?

Did you write this code or trying to modify this code?

This is a bit confusing. It also looks like you are doing some unnecessary procedure.

You know that you can do this:

$sql = "UPDATE Hit_Count SET HIT_COUNT = HIT_COUNT + 1 WHERE Page_Name = '$page'";

Instead of the whole "$any" thing you're doing, I would just check whether the number of rows returned was greater than "0". If not, then insert the new row.

    $page = $_SERVER['REQUEST_URI'];

    $result = mysql_query("SELECT HIT_COUNT FROM  Hit_Count WHERE Page_Name = '$page'");
    $num_rows = mysql_num_rows($result);

    if($num_rows != 0) {

        while($row = mysql_fetch_array($result)) {
            $sql = "UPDATE Hit_Count SET HIT_COUNT = HIT_COUNT + 1 WHERE Page_Name = '$page'";
            mysql_query($sql);
            echo($counter); 
        }

    }
    else
    {

        $sql = "INSERT INTO Hit_Count (Page_Name, HIT_COUNT) VALUES ('$page', '1')";
        mysql_query($sql);
        echo("1");

    }

Or you could check if the array returned "false":

    if(mysql_fetch_array($result) == false)
    {
        // do stuff
    }
Member Avatar for diafol

Why is your code all green?
It shouldn't be green at all.

It was due to a ` backtick at the start of the post. I've removed it, so it should look Ok now. You sure you haven't got OCD LM?

I was in my car when I read that ---^ and broke out into laughter.

Sorry to be really confusing. I was trying to match my code to be like a previous program earlier and as soon as I left this page I actually made some progress a different way. I wasn't sure if I should start a new thread or just show what I started from scratch on this page....so here is my hit counter code. The first bit of code would actually be the second file after the table itself is created.

<?php

$user_name = "";
$password = "";
$database = "";
$TableName = "";

$DBConnect = @mysqli_connect("", "", "", $)
        Or die("<p>Unable to connect to the database server.</p>"
        . "<p>Error code " . mysqli_connect_errno()
        . ": " . mysqli_connect_error()) . "</p>";
    $DBName = "";

if(! $DBConnect )
{
  die('Could not connect: ' . mysql_error());
}




mysql_select_db($DBName) or die("Cannot open $db: ".mysql_error());


mysql_query("UPDATE countertable SET count=count+1");


$counter = mysql_query("SELECT * FROM countertable");


print "[table border=1 cellpadding=3 cellspacing=0 width=80]"; 

while ($get_count = mysql_fetch_row($counter))
{ 
print "[tr]"; 
foreach ($get_count as $field) 
print "[td align=right][font> 
print "[/tr]"; 
print "[/table]"; 
}

mysql_close($DBConnect);
?>

</body>
</html>

and then my second file which actually creates the table looks like this.....

<?php

$user_name = "";
$password = "";
$database = "";
$TableName = "";

$DBConnect = @mysqli_connect("", "", "", $)
        Or die("<p>Unable to connect to the database server.</p>"
        . "<p>Error code " . mysqli_connect_errno()
        . ": " . mysqli_connect_error()) . "</p>";
    $DBName = "";

if(! $DBConnect )
{
  die('Could not connect: ' . mysql_error());
}
echo "<p>Connected to database successfully</p>";


mysql_select_db($DBName) 
or die("Select DB Error: ".mysql_error());

mysql_query("CREATE TABLE countertable( count INT(8))")or die("Create table Error: ".mysql_error());


mysql_close($DBConnect);

?>

</body>
</html>

The count visits file also still needs to instantiate the hit counter file as mentioned above....sorry to be so lengthy and confusing about this whole deal!

The new code just brings up more questions.

I'm not sure I understand why you are using different files for something that seems it should be all in the same. There are a lot of different syntax errors in the first file.

Why are you creating the table, shouldn't your table already exist?

It seems like the description of what you were doing before is no longer really relevant to the code you have now. This is updating a table with only one column.

I'm half asleep, so excuse me if I am asking anything that is obvious.

Here are some things I found to be a proble right away (again half asleep, so I am sure there are more).

<?php
$user_name = "";
$password = "";
$database = "";
$TableName = "";
$DBConnect = @mysqli_connect("", "", "", $) // using mysqli_connect but your queries and db connection close are using mysql_query and mysql_close
        Or die("<p>Unable to connect to the database server.</p>"
        . "<p>Error code " . mysqli_connect_errno()
        . ": " . mysqli_connect_error()) . "</p>";
    $DBName = ""; // what is this for? You have $database already set above.
if(! $DBConnect )
{
  die('Could not connect: ' . mysql_error());
}

// I'm not seeing where you are defining the variable $db. Should it be $database rather?
mysql_select_db($DBName) or die("Cannot open $db: ".mysql_error());

// won't you need a WHERE to tell which row to update, or is it really only one columns and one row?
mysql_query("UPDATE countertable SET count=count+1"); 

// I'm not even sure what is happening below this line except you are pulling some data.
// The the while loop and foreach looked pretty messed up. Took a guess and moved some stuff around.
$counter = mysql_query("SELECT * FROM countertable");
print "[table border=1 cellpadding=3 cellspacing=0 width=80]"; 
while ($get_count = mysql_fetch_row($counter))
{
    print "[tr]";
    foreach ($get_count as $field)
    {
        print "[td align=right][font>"; // you were missing a close double quote and semicolon.
    }
    print "[/tr]"; // moved this out of the foreach as the opening tr tag was outside as well.

}
print "[/table]"; // this was in the loop, I moved it out like the opening table tag.

mysql_close($DBConnect);
?>

Thank you! I got it working. I just went step by step through your help and everything worked. I am sure there are little things left so I am gonna go through it again tonight, but thank you for your help!

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.