MySQL,查询计数结果并在PHP页面中显示

时间:2014-09-08 22:33:17

标签: php html mysql sql

我有以下查询。我需要计算返回多少项/行,然后在运行此查询的PHP页面上显示该数字。

$query1 = "
SELECT HD_TICKET.ID as ID, 
HD_TICKET.TITLE as Title, 
HD_STATUS.NAME AS Status, 
HD_PRIORITY.NAME AS Priority, 
HD_TICKET.CREATED as Created, 
HD_TICKET.MODIFIED as Modified, 
HD_TICKET.CUSTOM_FIELD_VALUE10 as SLA,
S.FULL_NAME  as Submitter, 
O.FULL_NAME  as Owner, 
HD_TICKET.CUSTOM_FIELD_VALUE0 as Type  
FROM HD_TICKET  
JOIN HD_STATUS ON (HD_STATUS.ID = HD_TICKET.HD_STATUS_ID) 
JOIN HD_PRIORITY ON (HD_PRIORITY.ID = HD_TICKET.HD_PRIORITY_ID) 
LEFT JOIN USER S ON (S.ID = HD_TICKET.SUBMITTER_ID) 
LEFT JOIN USER O ON (O.ID = HD_TICKET.OWNER_ID) 
WHERE (HD_TICKET.HD_QUEUE_ID = $mainQueueID) AND 
(HD_STATUS.NAME like '%Open%')  
ORDER BY ID DESC
";

这可能吗,我不是MySQL友好的。基本上如果我可以输出计数,我可以把它拉到一个表中,让我用PHP / HTML / CSS刷新它。

感谢您的时间,感谢您的任何帮助。

1 个答案:

答案 0 :(得分:1)

如果您使用的是mysql扩展程序,则可以使用mysql_num_rows()获取结果中的行数。

$row_count = mysql_num_rows($result1);
echo "There are $row_count results</b>";
while ($row = mysql_fetch_assoc($result1) {
    // Display row of results
}

如果您不想显示结果,则应简化查询。您不必指定要返回的列,也不需要对结果进行排序。

$query = "SELECT COUNT(*) as cnt
            FROM HD_TICKET  
            JOIN HD_STATUS ON (HD_STATUS.ID = HD_TICKET.HD_STATUS_ID) 
            JOIN HD_PRIORITY ON (HD_PRIORITY.ID = HD_TICKET.HD_PRIORITY_ID) 
            LEFT JOIN USER S ON (S.ID = HD_TICKET.SUBMITTER_ID) 
            LEFT JOIN USER O ON (O.ID = HD_TICKET.OWNER_ID) 
            WHERE (HD_TICKET.HD_QUEUE_ID = $mainQueueID) AND 
            (HD_STATUS.NAME like '%Open%')";
$result1 = mysql_query($query);
$row = mysql_fetch_assoc($result1);
$row_count = $row['cnt'];
echo "$row_count results";