按字母顺序排序MySQL结果

时间:2014-01-26 17:30:14

标签: php mysql

我正在建立一个在线图书数据库,我有这个PHP脚本来搜索mysql数据库并获得结果。出于某种原因,结果是随机的。是否有人按作者对它们进行排序? (作者的名称字段=表单中的search_author和mysql列中的作者。)

    $search_titles=$_POST["search_titles"];
$search_authors=$_POST["search_authors"];
$search_shelf=$_POST["search_shelf"];
$search_genre=$_POST["search_genre"];

////////////////////////////////////////////////////////////////
//build the mySQL query string. Required something like this (the uppercase kwords are SQL commands): 
//SELECT * FROM booklist WHERE Title LIKE 'Computing' AND Author LIKE 'borg'
//Instead of AND, you might want to try OR

$query_string ="SELECT * FROM booklist WHERE ";

$insert_AND = false;  //a flag to indicate when an AND keyword needs to be inserted

//only include search terms/fields that have been specified in form (not empty, but note that a bunch of spaces will be treated as non-empty ... to-fix by validating form)
if ($search_titles != ""){
    $query_string .= "Title LIKE '%".$search_titles."%'";    //the % is a wild card
    $insert_AND = true;
}   

if ($search_genre != ""){
    $query_string .= "Genre LIKE '%".$search_genre."%'";    //the % is a wild card
    $insert_AND = true;
}   

if ($search_authors != ""){
    if ($insert_AND) $query_string .= " AND ";    //you might want to 
    $query_string .= "Author LIKE '%".$search_authors."%'"; 
    $insert_AND = true;
}       

if ($search_shelf != ""){
    if ($insert_AND) $query_string .= " AND ";
    $query_string .= "ShelfNumber LIKE '%".$search_shelf."%'"; 
}       

//echo($query_string);
//done building the query string
//////////////////////////////////////////////////////////////////////

//perform the query on the db using the query string 
$result_array = mysql_query($query_string )or die(mysql_error());

//The sql server will return a 2D array containing the results (if any).
//Output these to the browser
if(mysql_num_rows($result_array)==0){  //i.e. an empty array was returned, meaning no results
    echo "NO MATCHES FOUND";
}else{      //mathces found

    echo mysql_num_rows($result_array) . " results found <BR><HR>";     

    while ($row = mysql_fetch_assoc($result_array)) {   //loop through the array, each time displaying a result from the next row, until all rows have been displayed
        echo "<B>Title: </B>" . $row['Title'] . "<BR>";
        echo "<B>Author: </B>" . $row['Author']. "<BR>";
        echo "<B>Genre: </B>" . $row['Genre']. "<BR>";
        echo "<B>Shelf: </B>". $row['ShelfNumber']. "<BR>";
        //note that the other fields (e.g. genre, etc) are also in the array since in the query string '*' (i.e. all fields) was specified.
        echo "<HR>";
    }

}

2 个答案:

答案 0 :(得分:1)

在您的查询结束时,请输入:

ORDER BY search_author ASC

答案 1 :(得分:1)

作者 订购,您需要在查询中添加ORDER BY子句:

if ($search_authors != ""){
    if ($insert_AND) $query_string .= " AND ";    //you might want to 
    $query_string .= "Author LIKE '%".$search_authors."%' ORDER BY Author ASC"; 
    $insert_AND = true;
}

MySQL Documentation: Sorting options

参考现有的答案,正如一位提问者所说,它被错误地理解:

  

表单中的作者字段= search_author的名称和mysql列中的作者。

应该是ORDER BY Author ASC而不是ORDER BY search_author ASC

编辑1:

为了使其在添加查询时正常工作,您应将if ($search_authors != ""){置于if ($search_shelf != ""){

之下