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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.