Jump to content

Can't get MySQL count to show in PHP


RhysDamagnez

Recommended Posts

Hi, sorry for poor code. I am new.

 

I get the error Notice:

 

Undefined index: COUNT(*) in C:\xampp\htdocs\money\index2.php on line 36

 

Line 36 being "

echo '<td><strong>'.$data['COUNT(*)'].'</strong></td>';

"

 

$db=@mysql_pconnect($host.($port==0?'':':'.((int)$port)),$username,$password);
if(!$db || !@mysql_select_db($database,$db)){echo '<p>MySQL Error: '.mysql_error().'</p>'; exit();}
  
$sql = mysql_query('SELECT user, COUNT(*) TotalCount FROM '.$table.' GROUP BY minecraft_user HAVING COUNT(*) > 1 ORDER BY COUNT(*) DESC');

//Define first rank.
$rank = 1;
?>

<center>
<h1><u>iConomy Player Stats</u></h1>
<table border="0" cellpadding="3" cellspacing="10" id="minimalist">
<tr>
<td width="30"><strong>Username</strong></td>
<td width="30"><strong>Voted Times</strong></td>
<td width="30"><strong>Rank</strong></td>
</tr>
<?php 
while($data = mysql_fetch_assoc($sql)) {
   //Loops data and displays it in a table.
echo '<tr>';
echo '<td><strong>'.$data['user'].'</strong></td>';  
echo '<td><strong>'.$data['COUNT(*)'].'</strong></td>';
echo '<td><strong>'.$rank++.'</strong></td></tr>';
}  
?>
</table></center>

Link to comment
https://forums.phpfreaks.com/topic/263789-cant-get-mysql-count-to-show-in-php/
Share on other sites

looks like from your sql query you meant to alias the COUNT(*) operation as TotalCount, but are missing the as:

 

$sql = mysql_query('SELECT user, COUNT(*) TotalCount FROM '.$table.' GROUP BY minecraft_user HAVING COUNT(*) > 1 ORDER BY COUNT(*) DESC');

 

should be

 

$sql = mysql_query('SELECT user, COUNT(*) as TotalCount FROM '.$table.' GROUP BY minecraft_user HAVING TotalCount > 1 ORDER BY TotalCount DESC');

 

and you should be using $data['TotalCount'] in your echo

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.