PHP查询MySQL,显示“;count(*)”;


PHP querying MySQL, showing "count(*)"

我在工作中继承了一个旧系统,php 4.4和MySQL,我们从中运行我们的服务台软件,我要到明年才能升级任何东西。不过,我正在为一些事情而挣扎。我需要显示2到1小时前记录的通话总数。在数据库中,记录的每个调用的unix时间戳在"logdatex"列中在我的php中,我有以下

$OneHourAgo = strtotime('-1 hour'); //time 1 hour ago as Unix Timestamp
$TwoHoursAgo = strtotime('-400 hour'); // time 2 hours ago
$Test = mysql_query("select count(*) from opencall where logdatex between $OneHourAgo and $TwoHoursAgo") or die(mysql_error());

现在,在MySQL查询浏览器中,如果我输入查询,但用实际数字替换变量(我做了一个回声来获得数字),它会正常工作,并返回所需的数字:

从opencall中选择count(*),其中logdatex介于1326767703和1386764103

(上面没有使用1小时的样本,更像是几年)请你帮我把数字输入一个变量,我不知道怎么做。感谢的任何帮助

mysql_query不直接返回查询结果。相反,它返回一个结果资源。http://www.php.net/manual/en/function.mysql-query.php

因此,您需要使用mysql_fetch_row来获得结果。

http://www.php.net/manual/en/function.mysql-fetch-row.php

$row = mysql_fetch_row($Test);
$count = $row[0];

如果您有多个行,您将循环直到mysql_fetch_row返回false。但既然你知道你只会得到一排,你就可以这样做。

您应该更改查询:

select count(*) from opencall where logdatex between $TwoHoursAgo and $OneHourAgo

因为http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between说,它应该在最小值和最大值之间。

我不一定能理解你的意思,但可能是这样吗?

$OneHourAgo = strtotime('-1 hour'); //time 1 hour ago as Unix Timestamp
$TwoHoursAgo = strtotime('-2 hour'); // time 2 hours ago

链接:http://php.net/manual/es/function.strtotime.php