Home:ALL Converter>strange mysql behaviour on timestamp

strange mysql behaviour on timestamp

Ask Time:2012-02-21T16:57:09         Author:Andreas

Json Formatter

Please have a look at this mysql query. What it should do is pretty simple - list dates, created from timestamps not older than 10 days.

It works, but not perfectly ...

  • If I have only 1 timestamp matching, I have 0 results.
  • If I have 2 timestamps matching, I have 1 results.
  • if I have 3 timestamps matching, I have 2 results
  • ... and so on...

So the newest timestamp in the table is always ignored by the query, WHY ?!

$timestamp_now = date('U');
$timestamp_10_day_back = $timestamp_now - 864000;

mysql_select_db("$db_visitors");
$sql = "SELECT DATE(FROM_UNIXTIME(visitors_timestamp))
        FROM visitors
        WHERE visitors_timestamp > $timestamp_10_day_back
        ORDER BY visitors_timestamp DESC";
$sql = mysql_query($sql);
$row = mysql_fetch_array($sql);

while($row = mysql_fetch_array($sql)) {
    echo $row[0] . "<br>";
}

Author:Andreas,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/9374637/strange-mysql-behaviour-on-timestamp
Vytautas :

Just remove\n\n$row = mysql_fetch_array($sql);\n\n\n...which is swallowing your first result",
2012-02-21T09:00:44
Ashraf :

First row is being ignored because of the row $row = mysql_fetch_array($sql); then you call it again in your while loop . just remove this row .",
2012-02-21T09:05:36
yy