PHP fetch_array / sql without sorting values

SQL:

SELECT question,alt_1,alt_2,alt_3,alt_4 FROM questions WHERE id IN ('12','2','32','23')

      

PHP:

while ($questions = mysql_fetch_array($game)){
    echo "$questions[question]";
}

      

I want the lines to be printed in the order that the values ​​are sent to sql: 12 - 2 - 32 - 23.

But fetch_array / sql (?) Seems to sort the values, so the order becomes 2 - 12 - 23 - 32 (starts with the lowest number ..)

Is there a way to stop sorting the array by numbers ??

+3


source to share


2 answers


USE FIND_IN_SET

SELECT question,alt_1,alt_2,alt_3,alt_4 
FROM questions 
WHERE id IN ('12','2','32','23') 
ORDER BY FIND_IN_SET(id, '12,2,32,23')

      



Note FIND_IN_SET (id, '12, 2,32,23 ') 12,2 no space

FIND_IN_SET(field, 'val1,val2')

      

0


source


Try yhis,
  "ORDER BY FIELD (id'12 ',' 2 ',' 32 ',' 23 ')"



          SQL: "SELECT question,alt_1,alt_2,alt_3,alt_4 FROM questions WHERE id IN 
          ('12','2','32','23') ORDER BY FIELD(id'12','2','32','23')"

           while($questions = mysql_fetch_array($game)){
           echo "$questions[question]";
       }

      

0


source







All Articles