简体   繁体   中英

PHP/MySQL: Order artist by ASC, then order songs by artist by ASC?

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

I tried using while loops to order first the artist by AZ, then the songs by the artist AZ, 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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM