Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用JSON对mySQL表输出进行排序的PHP脚本_Php_Json - Fatal编程技术网

用JSON对mySQL表输出进行排序的PHP脚本

用JSON对mySQL表输出进行排序的PHP脚本,php,json,Php,Json,对于PHP和JSON非常陌生,我使用以下内容来显示行列表: $db = new PDO('mysql:host=localhost;dbname=rugbysuperleague','xxx','xxx'); $stmt = $db->query("SELECT * FROM `table`"); $data = $stmt->fetchAll(PDO::FETCH_ASSOC); $stmt->execute(); echo json_encode($data); 它返回表

对于PHP和JSON非常陌生,我使用以下内容来显示行列表:

$db = new PDO('mysql:host=localhost;dbname=rugbysuperleague','xxx','xxx');
$stmt = $db->query("SELECT * FROM `table`");
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->execute();
echo json_encode($data);
它返回表中的所有行OK,但如何返回基于特定列的aAscending值排序的行,如果是tie,则按下一列排序。等等


我正试图以正确的顺序列出一张运动队排名表。因此,得分最多的团队处于第一位,如果出现平局,则按点数差异排序等。

使用
按顺序排序
,并按您希望的顺序列出列:

SELECT * FROM `table` ORDER BY `points` DESC, `point_diff` ASC

您也不需要在sql查询中使用
execute()
,也可能不需要
fetchAll()

order by
?json只是一个字符串。它没有“秩序”。如果你想让json有序,你就必须按照你想要的顺序来构建你的JS数据结构,而且也不能保证你在json字符串中填充的内容会按照与原始JS结构相同的顺序。