2012-02-18 24 views
0

我需要一些幫助整理我的分數值是我的SQL數據庫使用PHP腳本進行排序SQL數據庫條目

<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> 

我嘗試從我的遊戲中發佈用戶的分數表,從最高排序它們來最低。我只是卡住..

回答

4

變化

$mystatement = mysql_query("SELECT * FROM people"); 

$mystatement = mysql_query("SELECT * FROM people ORDER BY score DESC"); 

取出數組排序

arsort($selection); 
ksort($nameselection); 
+0

+1提醒OP刪除PHP排序。 – Umbrella 2012-02-18 23:11:01

+0

美麗,我是新來的SQL,甚至不知道你可以做到這一點。非常感謝 – 2012-02-18 23:12:51

+0

[教程](http://dev.mysql.com/doc/refman/5.5/en/tutorial.html)可能很有用。 – 2012-06-20 14:01:37

2

使用ORDER BY在SQL查詢這樣的:

SELECT * FROM people ORDER BY score DESC 
1

可以DB-端使用 「排序」

即對其進行排序。 SELECT * FROM people ORDER BY scores desc

2
$mystatement = mysql_query("SELECT * FROM people ORDER BY score DESC"); 
+0

即使你是第三個回答+1,你是第一個包含正確的字段名稱,sco重新' – Umbrella 2012-02-18 23:10:01