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.