2

I have two different codes for getting the position of a student's score in two subjects. From result columns 'maths' and 'english' containing the scores of different users.

This is what the result for a user looks like

Subject     Score   Rank

English       5         7

Maths          0         1

While this what I hope to get:

Subject     Score   Rank

English       5         7

Maths          0

Here are the codes I used

FOR ENGLISH

<?php $sql="SELECT roll, FIND_IN_SET( English, (SELECT GROUP_CONCAT( English
ORDER BY English DESC ) 
FROM result )
) AS rank
FROM result
WHERE roll = '$roll'";

$result = mysql_query($sql);


$row = mysql_fetch_array($result, MYSQL_ASSOC);

echo $row['rank'] ?>

FOR MATHS

<?php $sql="SELECT roll, FIND_IN_SET( maths, (SELECT GROUP_CONCAT( maths
ORDER BY mathsDESC ) 
FROM result )
) AS rank
FROM result
WHERE roll = '$roll'";

$result = mysql_query($sql);


$row = mysql_fetch_array($result, MYSQL_ASSOC);

echo $row['rank'] ?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Charles Nwankwo
  • 31
  • 1
  • 1
  • 5
  • 1
    Would you be able to prepare a SQL Fiddle for us, so we can see you input data? Please also add your input data into the question, as a formatted block (you don't need
    and   here - just. use a code block, as per your SQL).
    – halfer Dec 22 '16 at 08:14
  • You may have a look at http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php and http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php (your code is vulnerable and use deprecated functions) – rap-2-h Dec 22 '16 at 08:14
  • ORDER BY English- eh? – Strawberry Dec 22 '16 at 08:30

0 Answers0