为什么我的 MySQL TIMESTAMPDIFF 不工作?

Why is my MySQL TIMESTAMPDIFF not working?

你好,我有一个存储的时间戳 "oldtime",我想得到与当前时间的差异,但它不起作用,也没有抛出任何错误,所以我迷路了。

$timediff = mysql_query(sprintf("SELECT TIMESTAMPDIFF(SECOND, %s, NOW())", GetSQLValueString($oldtime, "text")), $DBName) or die (mysql_error());

$diff = mysql_fetch_assoc($timediff);

echo '<script>console.log("time diff:'.$diff[0].'");</script>';

.$diff. results in "time diff:Array"

.$diff[0]. results in "time diff: "

.$timediff. results in "time diff:Resource id #6"

mysql_fetch_assoc 默认 returns 按列名而不是列号索引的数组。

列名就是函数字符串,类似于

TIMESTAMPDIFF(table.col1, NOW())

这里更简单的方法是按列号获取,而不是使用 mysql_fetch_row 或 mysql_fetch_array

见: http://php.net/manual/en/function.mysql-fetch-array.php http://php.net/manual/en/function.mysql-fetch-array.php

另一种方法是使用一些数组函数来提取没有名称的元素,请参阅: http://php.net/manual/en/ref.array.php