0

I have 2 MySQL tables, one for storing albums and the other for songs. I am displaying a list of albums in a table, and I want to be able to add a column called songs to display the number of songs in this album. The way I have it now just messes up my table:

Thanks for everyone's help!

8
  • 2
    what makes you to not write code ? Commented Nov 18, 2012 at 14:33
  • Could you please put the code in, instead of a screenshot of the highlighted code? Commented Nov 18, 2012 at 14:33
  • 1
    Can you show some rendered HTML? Commented Nov 18, 2012 at 14:34
  • 2
    Why do you want to see HTML when the question is about MySQL? Commented Nov 18, 2012 at 14:35
  • 1
    What is wrong with your current query? What are you trying to achieve? Commented Nov 18, 2012 at 14:37

1 Answer 1

1

Assuming that "playlist" is what you consider an album and the first while loop iterates on playlists, I'd rewrite your code like this:

while($row = mysql_fetch_array($rs)) {
    echo "<tr>\n";
    echo "<td>".$row['playlist_id']."</td>";

    // Assuming playlist_id is an integer value in your database
    $query = "
        SELECT Playlist_id, COUNT(Playlist_id) AS songCount
        FROM ws_music 
        WHERE Playlist_id = ". intval ($row['playlist_id']) ."
        GROUP BY Playlist_id 
    ";
    $result = mysql_query($query) or die(mysql_error());
    // No need for the second while loop
    $row2 = mysql_fetch_array($result);
    echo "<td>There are ". $row2['songCount'] ." ". $row2['Playlist_id'] ." song.</td>";
    echo "</tr>";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.