1

I am working on a music player and I'd like to list all of the songs sorted by artist going from A-Z (ASC). The information is stored in a table with artist, file, song, etc.

I tried using while loops to order first the artist by A-Z, then the songs by the artist A-Z, but that didn't work. I never completely understood any kind of loops, so help would be appreciated!

One of the queries I used was SELECT * FROM musicinfo WHERE user='$user' ORDER BY artist ASC.

halfer
  • 19,824
  • 17
  • 99
  • 186
alexpja
  • 556
  • 2
  • 5
  • 20
  • is there error? what is not working in your query? – John Woo Jan 26 '13 at 08:05
  • The query looks fine, what's the problem? – thordarson Jan 26 '13 at 08:09
  • After I sort by artist, I want to sort ASC by filename, except somehow in a while loop? I don't know how to do it correctly, although the query will be `SELECT * FROM musicinfo WHERE artist='$artist' ORDER BY file ASC` (I believe) – alexpja Jan 26 '13 at 08:11

1 Answers1

3

so you need multiple sort right? just add it on you ORDER BY clause.

SELECT * 
FROM   musicinfo 
WHERE  user = '$user' 
ORDER  BY artist ASC, file ASC

As a sidenote, the query is vulnerable with SQL Injection if the value(s) came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • So will it be like "Arriving Somewhere But Not Here" by Porcupine Tree, then "Time Flies" by same artist, then "01 New Generation Slave" by "Riverside", then "Index" by "Steven Wilson"? – alexpja Jan 26 '13 at 08:16
  • 1
    Just ran it through mysql, it worked! Wow, simple addition to my existing query and I would've had it. It's 2AM haha. Thanks! – alexpja Jan 26 '13 at 08:19
  • 2
    No you can't sleep. First you have to reimplement your code in either PDO or MySQLi... :) – Ian Atkin Jan 26 '13 at 09:22