Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 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
";

Is this possible, I am not MySQL friendly. Basically if I can output the count number I can then pull that into a table for me to flash it up with PHP / HTML / CSS.

I appreciate any help in advance thanks for your time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
190 views
Welcome To Ask or Share your Answers For Others

1 Answer

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";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...