使用PHP脚本对sql数据库条目进行排序
我需要一些帮助排序我的得分值的我的sql数据库使用PHP脚本对sql数据库条目进行排序,php,html,sql,sorting,Php,Html,Sql,Sorting,我需要一些帮助排序我的得分值的我的sql数据库 <table border = '2'> <?php $con = mysql_connect("localhost", "root", ""); if(!$con) { echo "Could not establish connection to the database " . mysql_error(); } $mydb = mysql_selec
<table border = '2'>
<?php
$con = mysql_connect("localhost", "root", "");
if(!$con)
{
echo "Could not establish connection to the database " . mysql_error();
}
$mydb = mysql_select_db("users", $con);
if (!$mydb)
{
echo "Error selecting database " . mysql_error();
}
$mystatement = mysql_query("SELECT * FROM people");
$dbcount = mysql_num_rows($mystatement);
$selection = array(0 => "");
$nameselection = array(0 => "");
$i = 0;
while ($row = mysql_fetch_array($mystatement))
{
$selection[$i] = $row['score'];
$nameselection[$i] = $row['username'];
//echo "<tr> <td>$row[username]</td> <td>$row[score]</td></tr>";
$i++;
}
arsort($selection);
ksort($nameselection);
for ($x = 0; $x < $dbcount; $x++)
{
echo "<tr> <td>$nameselection[$x]</td> <td>$selection[$x]</td> </tr>";
}
?>
</table>
我试着把我游戏中用户的分数贴到一张桌子上,从最高到最低排序。我只是被卡住了..在SQL查询中使用如下内容:
SELECT * FROM people ORDER BY score DESC
您可以使用“排序依据”对它们进行DB端排序 也就是说,
从按分数排序的人员中选择*
更改
$mystatement = mysql_query("SELECT * FROM people ORDER BY score DESC");
$mystatement = mysql_query("SELECT * FROM people");
到
删除数组排序
arsort($selection);
ksort($nameselection);
+1尽管你是第三个回答的人,但你是第一个包含正确字段名的人,
score
beautifuly,我是sql新手,甚至不知道你能做到这一点。非常感谢,木栅可能会有用。
arsort($selection);
ksort($nameselection);