0

I have made a little system where SQL gets the first 5 games based on ranking. Now I want to use them freely with PHP. So the best way to do that imo is to give them each a value

$one = first result $two = second result etc.

but I can only get them all together. How do i split them up and use them freely?

<?php

$RankSql = 'SELECT *
            FROM games
            WHERE Genre LIKE "%'.$Genre.'%"
            ORDER BY Score DESC limit 5';

$Rankresult = mysql_query($RankSql);
    while ($RankOrder = mysql_fetch_array($Rankresult))
    {
        echo $RankOrder['Game'] . '<br>';
    };

?>
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Jordy Senssen
  • 171
  • 3
  • 16
  • 1
    Clue: look at the name of the function in the loop. What does it say? What is the type of the data returned from the query? How can you iterate through such types? – A. Abramov Jun 22 '15 at 18:46
  • 3
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 22 '15 at 18:47
  • http://stackoverflow.com/a/3442930/ – Funk Forty Niner Jun 22 '15 at 18:53
  • I don't really fancy PHP, but I need this little thingy for a little school assignment, but I really have no clue at all on how to do this. Not even after reading the link. – Jordy Senssen Jun 22 '15 at 19:18

1 Answers1

0

Store your results arrays in an array:

<?php

$RankSql = 'SELECT *
            FROM games
            WHERE Genre LIKE "%'.$Genre.'%"
            ORDER BY Score DESC limit 5';

$Rankresult = mysql_query($RankSql);

$games = array();

while($RankOrder = mysql_fetch_array($Rankresult))
{
    $games[] = $RankOrder;
};

// ex: third game (remember, arrays start at zero)
echo $games[2]['Game'];
?>
spenibus
  • 4,339
  • 11
  • 26
  • 35