在php中排序mysql结果

When I use the following code:

$result = queryMysql("SELECT * FROM games ORDER BY game ASC");
$num    = $result->num_rows;

for ($j = 0 ; $j < $num ; ++$j)
{

        $row = $result->fetch_array(MYSQLI_ASSOC);
        echo "<input type='checkbox' name='game' value='$row['id']'>$row['game']<br>";
}

the result is not shown alphabetically. it has Destiny first, and Battlefield last. Destiny has ID 1, and Battlefield has ID 11.

Why isn´t it being sorted? If I use the command in PhpMyAdmin I get the list back sorted.

(Sorry if this is too simple, but I didn´t find any solutions here. All refered to use ORDER BY, but that´s not working).

From MyPhpAdmin

From Webpage

New code changed to:

$result = queryMysql("SELECT * FROM games ORDER BY game ASC");

while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
    echo "<input type='checkbox' name='game' value='$row['id']'>$row['game']<br>";
}

Still not working. Could this be a server error?

Not sure what happened here, but after 8 hours of sleep and I reload the page, it´s sorting as it should (still using Maximus2012´s example). I guess it must have been a server glitch, since I didn´t change the code while sleeping.

I´ll give creds to Maximus2012 for his quick and good responds! (and thanks to all others too ofcourse!)

Replace this :

$num    = $result->num_rows;
for ($j = 0 ; $j < $num ; ++$j)
{
    $row = $result->fetch_array(MYSQLI_ASSOC);
    echo "<input type='checkbox' name='game' value='$row['id']'>$row['game']<br>";
}

with this: (get rid of $num and the for loop)

while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
     echo "<input type='checkbox' name='game' value='$row['id']'>$row['game']<br>";
}

and see if that works. You don't need for loop since you are not using the $j variable anywhere anyway. This answer is based on the assumption that your MySQL query is giving you correct result.

It looks like you are sorting by whatever your first field game rather than the field that actually contains the name of your game (can't tell what that is from the information given).

Just sort the proper field.

Run SET NAMES utf8; Be sure you have collation utf8_general_ci in your table and field. Check that field game has character set utf8

Then run this query just before your own query and see if this helps.

function queryMysql($query)
{
    global $connection;
    $connection->query("SET NAMES utf8");
    $result = $connection->query($query);
    if (!$result) die($connection->error);
    return $result;
}