Patrick Patrick - 6 months ago 18
PHP Question

PHP/MySQL Can't print column generated by query

I'm trying to build a "top 15" results based on a field called UID, works great except not printing the count column in PHP, it does in PHPMyAdmin though.

SELECT * , COUNT( UID ) AS Count
FROM UserReports
GROUP BY `UID`
ORDER BY Count DESC
LIMIT 15


Here's the Returning Array:

( [ID] => 18 [Date] => 2016-05-28 13:58:05 [Name] => a [UID] => a [Reason] => a [Staff] => Patrick [Count] => 2 )

( [ID] => 19 [Date] => 2016-05-28 13:58:07 [Name] => b [UID] => b [Reason] => b [Staff] => Patrick [Count] => 1 )


PHP Code:

if($result) {
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['UID'] . "</td>";
echo "<td>" . $row['Count'] . "</td>";
echo "</tr>";
print_r($row);
echo "<br>";

}


But the output doesn't show the count column for me and I can't quite work out why.

Missing Column

Thanks in advance.

Answer

Comment to answer, seeing it was the real problem.

You have 6 columns in the screenshot, but only 5 <td>'s in your loop. Look at your own screenshot; you have 2 and 1 counts in there. Look at your HTML source also; it is also a "tool".

What's happening here is that the iterated values have been shifted.

You will need to match them.