Do you not read what I write? Your code won't work, because it just won't. You're running 3 queries, none of which depend upon eachother, all of which do different things which assume something else....

Take this code (and only this code) and put it in test.php:

<?php
$db_host = 'localhost';
$db_user = '';
$db_pass = '';

$con = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db('counter', $con);

$sq = "SELECT id 
FROM `counter` 
WHERE MONTH(timespan)=MONTH(NOW())
	AND DAY(timespan)=DAY(NOW()) 
	AND YEAR(timespan)=YEAR(NOW())
	AND HOUR(timespan)=HOUR(NOW())";
$res = @mysql_query($sq);

if($res === FALSE)
{
	// No rows yet, we need to add the initial one
	$q = "INSERT INTO `counter` (hits, timespan) VALUES ('1', NOW())";
}
else
{
	// Already a row dealing with the current hour

// Get ID from that row:
$id = mysql_result($res, 0, 'id');
// Specify Update query
$q = "UPDATE `counter` SET hits=hits+1 WHERE id='".$id."'";
}

// Define which action we're taking (Update or Insert);
$action = substr($q, 0, strpos($q, ' ', 0));
$action = ucwords(strtolower($action));

$res = @mysql_query($q);
if(!$res)
{
	// Failed to update/insert
	die('Failed to '.$action.' value(s) in <em>counter</em>.  MySQL gave the following information:<br><strong>Error #:</strong> '.mysql_errno().'<br><strong><em>Error  :</em></strong> '.mysql_error());
}
else
{
	$cq = "SELECT * FROM `counter` ORDER BY id DESC";
	$res = @mysql_query($cq);
	// Successfully updated/inserted information
	echo 'Successfully '.$action.'ed the value(s) in <em>counter</em>.  The table output is below:<br><br>';
	echo "
<table border='1'>\n
\t<tr><th>id</th><th>hits</th><th>timespan</th></tr>\n";
	while($r = mysql_fetch_array($res))
	{
		echo "
\t<tr>\n
\t\t<td>".$r['id']."</td>\n
\t\t<td>".$r['hits']."</td>\n
\t\t<td>".$r['timespan']."</td>\n
\t</tr>\n\n";
	}
	echo "
</table>";
}

mysql_close($con);

?>

In your cPanel click on "CronJobs". Click "Advanced (Unix Style) Interface".
Make it look like:
| 00 | | | | | /home/virtual/jumphopper/fst/var/www/projects/aktest/test.php |

Then, that's it. You have to wait for the hour to roll over in order for it to update. You can run the script on its own to update manually.

EDIT
I updated the PHP code, and ran it on my server: works perfectly fine. Cron should work, but I'm not gonna test that.

    i get:

     Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 4 in /home/virtual/jumphopper/fst/var/www/projects/aktest/test.php on line 20

    🙁

      SOrry, had it working prior to hour change.... updated & fixed:

      <?php
      $db_host = 'localhost';
      $db_user = '';
      $db_pass = '';
      
      $con = mysql_connect($db_host, $db_user, $db_pass);
      mysql_select_db('counter', $con);
      
      $sq = "SELECT id 
      FROM `counter` 
      WHERE MONTH(timespan)=MONTH(NOW())
      	AND DAY(timespan)=DAY(NOW()) 
      	AND YEAR(timespan)=YEAR(NOW())
      	AND HOUR(timespan)=HOUR(NOW())
      LIMIT 1";
      $res = @mysql_query($sq);
      
      $r = mysql_fetch_array($res);
      
      if(!$r)
      {
      	// No rows yet, we need to add the initial one
      	$q = "INSERT INTO `counter` (hits, timespan) VALUES ('1', NOW())";
      }
      else
      {
      	// Already a row dealing with the current hour
      	// Specify Update query
      	$q = "UPDATE `counter` SET hits=hits+1 WHERE id='".$r['id']."'";
      }
      
      // Define which action we're taking (Update or Insert);
      $action = substr($q, 0, strpos($q, ' ', 0));
      $action = ucwords(strtolower($action));
      
      $res = @mysql_query($q);
      if(!$res)
      {
      	// Failed to update/insert
      	die('Failed to '.$action.' value(s) in <em>counter</em>.  MySQL gave the following information:<br><strong>Error #:</strong> '.mysql_errno().'<br><strong><em>Error  :</em></strong> '.mysql_error());
      }
      else
      {
      	$cq = "SELECT * FROM `counter` ORDER BY id DESC";
      	$res = @mysql_query($cq);
      	// Successfully updated/inserted information
      	echo 'Successfully '.$action.'ed the value(s) in <em>counter</em>.  The table output is below:<br><br>';
      	echo "
      <table border='1'>\n
      \t<tr><th>id</th><th>hits</th><th>timespan</th></tr>\n";
      	while($r = mysql_fetch_array($res))
      	{
      		echo "
      \t<tr>\n
      \t\t<td>".$r['id']."</td>\n
      \t\t<td>".$r['hits']."</td>\n
      \t\t<td>".$r['timespan']."</td>\n
      \t</tr>\n\n";
      	}
      	echo "
      </table>";
      }
      
      mysql_close($con);
      
      ?>

        do you know if its possible to get the results table to pageinate?

        and how if can grab and post results from a specific timespan?
        for example.

        100hits 2PM-3PM 12/12/06,
        96hits 4PM-5PM 12/12/06

        i cant thank you enough for your help. im slowly starting to understand how php works. its starting to make sense.

        also do i need to purchase a login for cPanel? i have never heard of cPanel before. i did my research. but it looks like i must subscribe

          would it be possible to look at my paginating code?
          it seems that when i navigate throughout the pages it seems to add another hit to the counter.

          <?php
          require('sql.php');
          
          //How many records to show per page
          $show = 20;
          
          //Check to see if we've calulated the number of pages yet
          if (isset($_GET['np'])) {
          	$num_pages = $_GET['np'];
          } else {
          	//Count the number of total records in the database query using the COUNT() SQL function
          	$query = "SELECT COUNT(*) FROM tracker ORDER BY start ASC";
          	$result = mysql_query($query);
          	$row = mysql_fetch_array($result, MYSQL_NUM);
          	$num_records = $row[0];
          
          	//Calculate the number of pages required to show all the results
          	if ($num_records > $show) {
          		$num_pages = ceil($num_records/$show);
          	} else {
          		$num_pages = 1;
          	}
          }
          
          //Figure out the first record to return from the database
          if (isset($_GET['s'])) {
          	$start = $_GET['s'];
          } else {
          	$start = 0;
          }
          
          
          $sq = "SELECT id 
          FROM `tracker` 
          WHERE MONTH(timespan)=MONTH(NOW()) 
              AND DAY(timespan)=DAY(NOW()) 
              AND YEAR(timespan)=YEAR(NOW()) 
              AND HOUR(timespan)=HOUR(NOW()) 
          LIMIT 1"; 
          $res = @mysql_query($sq); 
          
          
          if(!$r) 
          { 
              // No rows yet, we need to add the initial one 
              $q = "INSERT INTO `tracker` (home, start, end , timespan) VALUES ('','1','', NOW())"; 
          } 
          else 
          { 
              // Already a row dealing with the current hour 
              // Specify Update query 
              $q = "UPDATE `tracker` SET start=start+1 WHERE id='".$r['id']."'"; 
          } 
          
          // Define which action we're taking (Update or Insert); 
          $action = substr($q, 0, strpos($q, ' ', 0)); 
          $action = ucwords(strtolower($action)); 
          
          $res = @mysql_query($q); 
          if(!$res) 
          { 
              // Failed to update/insert 
              die('Failed to '.$action.' value(s) in <em>tracker</em>.  MySQL gave the following information:<br><strong>Error #:</strong> '.mysql_errno().'<br><strong><em>Error  :</em></strong> '.mysql_error()); 
          } 
          else 
          { 
              $cq = "SELECT * FROM `tracker` ORDER BY id DESC LIMIT $start, $show"; 
              $res = @mysql_query($cq); 
              // Successfully updated/inserted information 
              echo 'Successfully '.$action.'ed the value(s) in <em>tracker</em>.  The table output is below:<br><br>'; 
              echo " 
          <table border='1'>\n 
          \t<tr><th>id</th><th>home</th><th>start</th><th>end</th><th>timespan</th></tr>\n"; 
              while($r = mysql_fetch_array($res)) 
              { 
                  echo " 
          \t<tr>\n 
          \t\t<td>".$r['id']."</td>\n 
          \t\t<td>".$r['home']."</td>\n
          \t\t<td>".$r['start']."</td>\n 
          \t\t<td>".$r['end']."</td>\n  
          \t\t<td>".$r['timespan']."</td>\n \t</tr>\n\n"; } echo " </table>"; } //Make the numbered links for the other pages if ($num_pages > 1 ) { echo '<br><p>'; //Figure out what page the current script is now on $current_page = ($start/$show) +1; //Make a previous button if you are not on the first page of results if ($current_page != 1 ) { echo '<a href="run.php?s=' . ($start - $show) . '&np=' . $num_pages . '"><< </a>'; } //Make all the numbered links for ($i = 1; $i <= $num_pages; $i++) { if ($i != $current_page) { echo '<a href="run.php?s=' . (($show * ($i - 1))) . '&np=' . $num_pages . '">' . $i . '</a>'; } else { echo $i . ' '; } } //Make a next button if you are not on the last page of results if ($current_page != $num_pages) { echo '<a href="run.php?s=' . ($start + $show) . '&np=' . $num_pages . '"> >></a>'; } echo '</p>'; } ?>

            Search for pagination and compare.... that's a completely separate topic....

              Hi im currently trying to categorize the results by date.

              however my code displays this:

              Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/virtual/site23/fst/var/www/projects/aktest/date.php on line 92

              can you see anything wrong with my line 92?

              <p><strong>Sort Records By Date</strong>
              <form name="form1" method="post" action="<?php $_SERVER['PHP_SELF']?>">
              <table width="200" border="1">
              <tr>
              <td><label><strong>Month</strong></label></td>
              <td><label><strong>Date</strong></label></td>
              <td><label><strong>Year</strong></label></td>
              </tr>
              <tr>
              <td><select name="MONTH">
              <option value="01" selected="selected">January</option>
              <option value="02">Feburary</option>
              <option value="03">March</option>
              <option value="04">April</option>
              <option value="05">May</option>
              <option value="06">June</option>
              <option value="07">July</option>
              <option value="08">August</option>
              <option value="09">September</option>
              <option value="10">October</option>
              <option value="11">November</option>
              <option value="12">December</option>
              </select></td>
              <td><select name="DAY">
              <option value="01" selected="selected">01</option>
              <option value="02">02</option>
              <option value="03">03</option>
              <option value="04">04</option>
              <option value="05">05</option>
              <option value="06">06</option>
              <option value="07">07</option>
              <option value="08">08</option>
              <option value="09">09</option>
              <option value="10">10</option>
              <option value="11">11</option>
              <option value="12">12</option>
              <option value="13">13</option>
              <option value="14">14</option>
              <option value="15">15</option>
              <option value="16">16</option>
              <option value="17">17</option>
              <option value="18">18</option>
              <option value="19">19</option>
              <option value="20">20</option>
              <option value="21">21</option>
              <option value="22">22</option>
              <option value="23">23</option>
              <option value="24">24</option>
              <option value="25">25</option>
              <option value="26">26</option>
              <option value="27">27</option>
              <option value="28">28</option>
              <option value="29">29</option>
              <option value="30">30</option>
              <option value="31">31</option>
              </select></td>
              <td><select name="YEAR">
              <option value="2006" selected="selected">2006</option>
              </select></td>
              </tr>
              </table>
              <input name="submit" type="submit" value="submit"></form><br />
              <br />

              <?php

              require('sql.php');

              if (isset($POST['submit'])) {
              $DAY = $
              POST['DAY'];
              $MONTH = $POST['MONTH'];
              $YEAR = $
              POST['YEAR'];
              }

              if (isset($Submit))
              {
              $query = "SELECT * FROM tracker WHERE timespan LIKE '$YEAR-$MONTH-$DAY%'";
              $res = mysql_query($query);

              }
              echo "<table border='1'>\n
              \t<tr><th>id</th><th>home</th><th>start</th><th>end</th><th>timespan</th></tr>\n";
              while($row = mysql_fetch_array($res))
              echo "
              \t<tr>\n
              \t\t<td>".$row['id']."</td>\n
              \t\t<td>".$row['home']."</td>\n
              \t\t<td>".$row['start']."</td>\n
              \t\t<td>".$row['end']."</td>\n

              \t\t<td>".$row['timespan']."</td>\n
              \t</tr>\n\n";
              echo "
              </table>";

              ?>

                The problem is not with the mysql_fetch_array, but with your query. Your query isn't returning results, so there is no row to fetch to an array.

                It should work (i.e. I just tested your exact query with my table and it works (mySQL 5)). Just make sure that there is a date in the database that corresponds to the date you are querying for.

                This points out some flaws in your code: You should account for the fact that there might not be stats for a certain day yet. That's why you always check the mysql_num_rows result so if there aren't rows, you can go ahead and modify your code accordingly so you don't get errrors....

                  am i displaying the date right?

                  if (isset($_POST['submit']))  {
                  $YEAR = $_POST['YEAR']; 
                  $MONTH = $_POST['MONTH']; 
                  $DAY = $_POST['DAY'];
                  }
                  
                  
                  if (isset($Submit))
                  {
                  $query = "SELECT * FROM tracker WHERE timespan LIKE 'YEAR-MONTH-DAY%'";

                    brett, im totally lost 🙁...
                    here is my attempt to mysql_num_row... not looking very good. i have no clue where to start. i've never used this command before.

                    <?php
                    
                    require('sql.php');
                    
                    if (isset($_POST['submit']))  {
                    $YEAR = $_POST['YEAR']; 
                    $MONTH = $_POST['MONTH']; 
                    $DAY = $_POST['DAY'];
                    }
                    
                    
                    if (isset($Submit))
                    {
                    $result=mysql_query("SELECT * FROM tracker WHERE start = '1' ORDER BY id DESC LIMIT 1") or die ("Cannot execute the query");
                    //$query = "SELECT * FROM tracker WHERE timespan LIKE 'YEAR-MONTH-DAY %'";
                    $result = mysql_query($query);
                    
                    }
                    echo "<table border='1'>\n 
                    \t<tr><th>id</th><th>home</th><th>start</th><th>end</th><th>timespan</th></tr>\n";
                    if (mysql_num_rows($result)>0) { 
                        while($row=mysql_fetch_array($result)) { 
                    	echo " 
                    \t<tr>\n 
                    \t\t<td>".$row['id']."</td>\n 
                    \t\t<td>".$row['home']."</td>\n
                    \t\t<td>".$row['start']."</td>\n 
                    \t\t<td>".$row['end']."</td>\n  
                    \t\t<td>".$row['timespan']."</td>\n \t</tr>\n\n"; echo " </table>"; } } ?>

                      This:

                      "SELECT * FROM tracker WHERE timespan LIKE 'YEAR-MONTH-DAY%'";

                      should be:

                      "SELECT * FROM tracker WHERE timespan LIKE '".$YEAR."-".$MONTH."-".$DAY."%'";

                        i still get the error msg even with the SELECT update...
                        i dont see what im doing wrong... 😕

                          add an "or die(mysql_error())" statement to the query call.

                            awesome everything is working! 🙂
                            but can you further explain to me how to get the "mysql_num_rows" command to work?
                            so that i dont have a big list of 0-1-0's?

                            here is my current code

                            <?php
                            
                            require('sql.php');
                            
                            if (isset($_POST['submit']))  {
                            $YEAR = $_POST['YEAR']; 
                            $MONTH = $_POST['MONTH']; 
                            $DAY = $_POST['DAY'];
                            }
                            
                            
                            if (isset($submit))
                            {
                            $query = "SELECT * FROM tracker WHERE timespan LIKE '".$YEAR."-".$MONTH."-".$DAY."%'"; 
                            $res = mysql_query($query) or die("couldn't perform $query");
                            }
                            echo "<table border='1'>\n 
                            \t<tr><th>id</th><th>home</th><th>start</th><th>end</th><th>timespan</th></tr>\n";
                            
                            while($r=mysql_fetch_array($res)) { 
                                echo " 
                            \t<tr>\n 
                            \t\t<td>".$r['id']."</td>\n 
                            \t\t<td>".$r['home']."</td>\n
                            \t\t<td>".$r['start']."</td>\n 
                            \t\t<td>".$r['end']."</td>\n  
                            \t\t<td>".$r['timespan']."</td>\n \t</tr>\n\n"; } echo " </table>"; ?>

                              I'd love to!

                              One question:
                              Where are you using it, and how are you using it?

                                well basically, my current code counts the amount of clicks on my flash site.
                                so when someone clicks a link, it keeps record and displays in a table, like this:
                                (id:1)(home=0) (start=1) (end=0) 2006-01-26
                                (id:2)(home=1) (start=0) (end=0) 2006-01-26

                                (id:3)(home=0) (start=1) (end=0) 2006-01-26

                                (id:4)(home=0) (start=0) (end=1) 2006-01-26

                                but, i want to have it so it displays all the clicks of a certain section of that day
                                in one row, like this instead:
                                (id:1)(home=23) (start=0) (end=0) 2006-01-26
                                (id:2)(home=0) (start=12) (end=0) 2006-01-26

                                from my research mysql_num_rows seems to be the solution to this... but i dont know where to input that command in my code.
                                let me know if i'm being unclear.

                                any ideas?

                                  SELECT COUNT(home) AS c_home, COUNT(start) AS c_start, COUNT(end) AS c_end
                                  FROM tracker
                                  WHERE timespan LIKE '$year-$month-$day'

                                  Then reference each as:
                                  c_home = 23
                                  c_start = 12
                                  c_end = 0

                                  So you'd say:

                                  <?php
                                  $query = "SELECT COUNT(home) AS c_home, COUNT(start) AS c_start, COUNT(end) AS c_end
                                  FROM tracker
                                  WHERE timespan LIKE '".$year."-".$month."-".$day."'";
                                  
                                  $result = mysql_query($query) or die(mysql_error());
                                  $row = mysql_fetch_array($result);
                                  echo 'Home: '.$row['c_home'].'<br>Start: '.$row['c_start'].'<br>End: '.$row['c_end'];
                                  ?>