Jump to content

Need help with php mysql count syntax


jefffogel1974

Recommended Posts

I am building a web application that list band names in the right column and in the left column the number of songs in my DB for each band. But I cant get it to work correctly. Can anyone help with this and let me know what im doing wrong in the code.

 

URL

http://www.xtremetab.com/tablatureDB/test.php

 

 

Code

 

<?php

 

$data = mysql_query("SELECT DISTINCT AllBands FROM Allsongs WHERE SUBSTR(LCASE(AllBands),1,1) = LCASE('1') ORDER BY AllBands ASC LIMIT 1,50");

 

echo '<table width="100%">';

echo '<tr><td>Artists:</td><td># of songs:</td></tr>';

 

while($result = mysql_fetch_assoc($data)){

 

$_sql = mysql_query("SELECT DISTINCT(AllSongs) as SongCount FROM allsongs WHERE AllSongID='".$result['AllSongID']."'"); $count['SongCount'];

 

 

 

echo '<tr>';

echo '<td>'.$result['AllBands'].'</td>';

echo '<td>[ <strong>'.$count['SongCount'].'</strong> Songs ]</td>';

echo '</tr>';

 

 

}

echo '</table>';

 

?>

 

Link to comment
Share on other sites

you're not counting the songs at all?

or pulling the results into a variable..?

 

<?php

$data = mysql_query("SELECT DISTINCT AllBands FROM Allsongs WHERE SUBSTR(LCASE(AllBands),1,1) = LCASE('1') ORDER BY AllBands ASC LIMIT 1,50");

echo '<table width="100%">';
echo '<tr><td>Artists:</td><td># of songs:</td></tr>';

while($result = mysql_fetch_assoc($data)){

$_sql = mysql_query("SELECT DISTINCT(AllSongs) as SongCount FROM allsongs WHERE AllSongID='".$result['AllSongID']."'"); 

//count the number of rows returned by the previous SQL query
$songCount = mysql_num_rows($_sql);

echo '<tr>';
echo '<td>'.$result['AllBands'].'</td>';
echo '<td>[ <strong>'.$songCount.'</strong> Songs ]</td>';
echo '</tr>';


}
echo '</table>';

?>

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.