SQL每小时​​查询在php中工作

时间:2014-07-09 15:44:11

标签: php mysql sql

我有一个SQL查询,我从SQLYog / WorkBench运行,它从数据库中提取每小时返工信息并输出到表中。

我在下面使用的查询:

SELECT HOUR(requested)AS `Hour`
, `ManualAction_L`.`name` AS `Rework Type`
, requestedByUsername AS `User`, COUNT(`KitManualAction_T`.`id`) AS `Count`
FROM KitManualAction_T, ManualAction_L
WHERE `KitManualAction_T`.`manualActionID`= `ManualAction_L`.`id` AND requested BETWEEN '2013-07-24 05:00:00' AND '2013-07-24 14:00:00'  
GROUP BY `Hour`, requestedByUsername, manualActionID
ORDER BY `Hour` ASC;

我现在遇到的问题是我想通过php / cron自动化这个过程。我似乎无法让查询在php中工作,并在表格中输出信息,显示小时,类型,用户和计数。

我从SQLYog / Workbench获得的当前输出如下所示:

>Hour Type  User    Count
>5  Create  heidis  70
>5  Create  lilly   27
>5  Reprint Label   lilly   7
>5  Cease   lilly   6
>5  Create  meisies 78
>5  Reprint Label   meisies 5
>5  Create  ntsoanet    2
>6  Create  heidis  133
>6  Reprint Label   heidis  3
>6  Create  lilly   105
>6  Reprint Label   lilly   1

这是我正在玩的当前PHP代码

$con = new mysqli($host, $user, $password, $dbname, $port, $socket)
or die ('Could not connect to the database server' . mysqli_connect_error());

$query = "SELECT HOUR(requested)AS 'Hour' , `ManualAction_L`.`name` AS 'ReworkType' , requestedByUsername AS 'User', COUNT(`KitManualAction_T`.`id`) AS 'Count' FROM KitManualAction_T, ManualAction_L WHERE `KitManualAction_T`.`manualActionID`= `ManualAction_L`.`id` AND requested BETWEEN '2013-07-24 05:00:00' AND '2013-07-24 14:00:00'   GROUP BY `Hour`, requestedByUsername, manualActionID ORDER BY `Hour` ASC";

if ($stmt = $con->prepare($query)) {
    $stmt->execute();
    $stmt->bind_result($Hour, $ReworkType, $User, $Count);
    while ($stmt->fetch()) {
        printf("%s, %s, %s, %s\n", $Hour, $ReworkType, $User, $Count);
    }
    $stmt->close();
}

0 个答案:

没有答案