简体   繁体   English

如何在MySQL数据库中对行进行排序

[英]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 pk变量是表的主键
  • ocena variable is integer ocena变量是整数

Don't do SELECT pk FROM utisci ORDER BY ocena DESC in the body of the while loop. 不要在while循环的主体中通过SELECT pk FROM utisci ORDER BY ocena DESC执行SELECT pk FROM utisci ORDER BY ocena DESC Do it before the while loop instead of SELECT * FROM utisci . while循环之前执行此操作, while不要执行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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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