Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100



Go Back   PHP-Editors > Linux, Apache, MySQL > MySQL Help

MySQL Help Post any question relating to MySQL here and hopefully someone can help

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 2007-02-09, 10:58 PM
Junior Member
 
Join Date: Jan 2007
Posts: 12
Wilson is on a distinguished road
Default Syntax For Count help

I have a query that I cannot get to display. I have been through umpteen itirations of If, for and while PHP syntax, but cannot get the count(total) columm to display. As I only get to work on this on Saturdays, I am tired of not being able to figure this out. Can anyone tell me if what code to use to get the thing to display, and if the COUNT [total] requires a special type of code to display a columm that is adding numbers from the distinct artists?

Code:
$sql = 'SELECT DISTINCT `artist` , COUNT( `total` ) FROM `songs` GROUP BY `artist` ORDER BY `artist` asc ';
$result = mysql_query($sql, $dbh)
OR die ($sql . mysql_error());
$count= mysql_num_rows($result);
echo ("$count");
 
echo ("<table border=1>");
WHILE ($row = mysql_fetch_array($result))
{
echo ("<tr>");
FOR ($row = $artist[artist];
    $row = $total[total]);
echo ("<td>$row[$artist]</td>");
echo ("<td>$row[$total]</td>");
echo ("</tr>");
echo ("</table>");
}
This is my latest try, but it is apparent I am going backwards. The query returns the proper amount of rows (echo ("$count") and displays pefectly in phpmyadmin, but will not display on the web. When I remove the count function, displaying the table is no problem, but that negates the reason for the query


UPDATE, I got my answer by changing the query to this

Code:
$sql = 'SELECT DISTINCT `artist` , COUNT( `total` ) FROM `songs` GROUP BY `artist` ORDER BY `artist` asc ';
$result = mysql_query($sql, $dbh)
OR die ($sql . mysql_error());
$count= mysql_num_rows($result);
echo ("$count");

echo ("<table border=1 width=100%>");
WHILE (LIST($artist, $total) = mysql_fetch_array($result))
{
echo "<tr>\n" .
"<td>$artist</td>\n" .
"<td>$total</td>\n" .
"</tr>\n";
}
echo "</table>";
I used list instead of For

Last edited by Wilson; 2007-02-10 at 08:26 AM. Reason: clarification
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 2007-02-18, 02:46 PM
Member
 
Join Date: Jun 2006
Posts: 55
flann is on a distinguished road
Default

count is an aggregate function and cannot be used in this fashion. You'll need to do two separate queries, or use a sub query if available.
__________________
flann
Free Mortgage Calculator | Debt Free
Reply With Quote
  #3 (permalink)  
Old 2007-02-19, 12:31 PM
Junior Member
 
Join Date: Jan 2007
Posts: 12
Wilson is on a distinguished road
Default

Actually, the script as shown works very well, the 2nd one anyways. I have lots of MIDI music, and this script shows the artists and counts up the files per artist.

The script as show also works just fine in PhpMyAdmin as well.
Reply With Quote
  #4 (permalink)  
Old 2007-02-19, 12:56 PM
Member
 
Join Date: Jun 2006
Posts: 55
flann is on a distinguished road
Default

my appologies, i didn't think it would work at all.
__________________
flann
Free Mortgage Calculator | Debt Free
Reply With Quote
  #5 (permalink)  
Old 2007-02-26, 06:46 PM
Junior Member
 
Join Date: Jan 2007
Posts: 12
Wilson is on a distinguished road
Default

Thats cool man, I only made it work accidently. Its not at all like I know what i am doing.
Reply With Quote
Must read Review for Serious PHP Developers


NuSphere PhpED 5.5 : The Staff of php-editors.com recently spent a few days working with NuSphere PhpED 5.5 (a popular PHP IDE) and NuCoder 2.0 (a PHP Encoding Utility), read up on all the details.

Sponsored Links
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 11:34 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.