Mysql, Query Count Results And Display In Php Page
I have the below query. I need to count how many items/rows are returned and then display that number on the PHP page this query is run from. $query1 = ' SELECT HD_TICKET.ID as
Solution 1:
If you're using the mysql
extension, you can get the number of rows in the result with 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
}
If you don't want to display the results, you should simplify your query. You don't have to specify the columns to return, and you don't need to order the 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";
Post a Comment for "Mysql, Query Count Results And Display In Php Page"