简体   繁体   中英

How to sort rows in MySQL database

I have this code

$link = new mysqli('localhost', 'root', '', 'domaci2');
        $query = 'SELECT * FROM utisci';
        $result = $link->query($query);
        while ($row=$result->fetch_assoc()){
            $link->query('SELECT pk FROM utisci ORDER BY ocena DESC');
        }
        $result->free();
        mysqli_close($link);

It is very simple, i am just trying to sort my table but it is not working.
Extra info:

  • pk variable is the primary key of table
  • ocena variable is integer

Don't do SELECT pk FROM utisci ORDER BY ocena DESC in the body of the while loop. Do it before the while loop instead of SELECT * FROM utisci .

This should work

$link = new mysqli('localhost', 'root', '', 'domaci2');
$query = 'SELECT pk FROM utisci ORDER BY ocena DESC';
$result = $link->query($query);
while ($row=$result->fetch_assoc()){
   // what you want to do with $row
}
$result->free();
mysqli_close($link);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM