title: Get a rank from list of scores from MySQL database using PHP to an iPhone
body: <p>I am trying to receive a rank from list of scores in a MySQL database and send it to iPhone. I know how to send a value to iPhone but getting a rank from list of scores in a database is little beyond my ability right now.</p>
  
  <p>I have looked into "<a href="https://stackoverflow.com/questions/15137093/get-a-rank-based-on-score-from-an-unordered-mysql-database-when-given-a-userna">Get a rank, based on score, from an unordered MySql Database when given a Username</a>" and "<a href="https://stackoverflow.com/questions/3614666/mysql-get-row-position-in-order-by">MySQL get row position in ORDER BY</a>" and have used the second link's solution to try to come up with my own answer, but it just is not cutting it yet.</p>
  
  <p>In my php file, I receive score, user name (uname), user device id (udid) and using $_POST and enter it into my high score database. After inserting the score, I try to find the user's ranking by looking through scores and finding right 'position' as rank.</p>
  
  <p>My code is:</p>
  
  <pre><code>$rank = 0;
  $stmt = $this-&gt;db-&gt;prepare('SELECT t.id, (SELECT COUNT(*) FROM high_score_4 WHERE score&lt;=t.score) AS position t.score FROM TABLE t WHERE t.score=?');
  $stmt-&gt;bind_param("i",  $score);
  $stmt-&gt;execute();
  $stmt-&gt;bind_result($rank);
  while($stmt-&gt;fetch()){
       break;
  }
  $stmt-&gt;close();
  echo "rank is : " . $rank . "\r\n";
  </code></pre>
  
  <p>It's the prepare statement that I am quite confused about. I am not even sure if I understood the other post's answer correctly... especially the t.id and TABLE t part</p>
  
  <p>I am quite new to all PHP and MySQL stuff, so my code is most likely quite hectic and wrong fundamentally, but I am sure an experienced eye could probably tell what is wrong with my code immediately, and any help will be greatly appreciated!</p>
  
  <p>Here is my table structure for high_score_4 table:</p>
  
  <pre><code>+----+-------+------------+------------------+
  | id | score | uname      | udid             |
  +----+-------+------------+------------------+
  |1   |  2313 | one        |device-one        |
  |2   |  1923 | two        |device-two        |
  |3   |  3212 | three      |device-three      |
  +----+-------+------------+------------------+
  
  CREATE TABLE high_score_4 (
      id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
      score mediumint NOT NULL,
      uname varchar(255) NOT NULL,
      udid varchar(255) NOT NULL,
      puzzles tinyint NOT NULL,
      multiplier tinyint NOT NULL,
      oneshots tinyint NOT NULL,
      hints tinyint NOT NULL,
      time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
  );
  </code></pre>
  
  <p>so if I look for score of 2313, I want to receive rank 2. I don't need anything else!</p>
  
  <p>Thanks in advance!</p>
  
  <p>Problem solved thanks to Peter's help!
  Here is the new code:</p>
  
  <pre><code>$stmt = $this-&gt;db-&gt;prepare('SELECT rank FROM(SELECT id, score, @n := IF(@g = score, @n, @n + 1) rank, @g := score FROM high_score_4, (SELECT @n := 0) i ORDER BY score DESC)q WHERE score=?');
  $stmt-&gt;bind_param("i",  $score);
  $stmt-&gt;execute();
  $stmt-&gt;bind_result($rank);
  while($stmt-&gt;fetch()){
       break;
  }
  $stmt-&gt;close();
  echo "rank is : " . $rank . "\r\n";
  </code></pre>
  
  <p>and rank is properly printed!
  Thanks everyone for help!</p>