[英]Sort MySQL Results Alphabetically
我正在建立一個在線圖書數據庫,並且我有這個php腳本來搜索mysql數據庫並獲取結果。 由於某種原因,結果是隨機的。 有沒有人按作者排序? (在表單中為author field = search_author的名稱,在mysql列中為Author。)
$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>";
}
}
在查詢末尾輸入以下內容:
ORDER BY search_author ASC
為了按作者 排序 ,您需要在查詢中添加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;
}
正如發問者所言,提及現有答案時,它的理解不正確:
表單中author字段的名稱= search_author,mysql列中的Author的名稱。
它應該是ORDER BY Author ASC
而不是ORDER BY search_author ASC
。
編輯1:
為了使它在添加查詢的情況下正常運行,您應該在if ($search_authors != ""){
下面加上if ($search_shelf != ""){
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.