嗨,在不再次执行查询的情况下从查询中进行“排序”的最佳方法是什么?假设当我必须使用表单查找姓名或姓氏时,我有一个搜索查询,例如:
//Array for storing WHERE part of query
$where=array();
if(!empty($name)) AND !empty($surname)){
$where[]="Name LIKE '%$name%'" .
"AND Surname LIKE '%$surname%'";
}
elseif(!empty($name)){
$where[]="Name LIKE '%$name%'";
}
elseif(!empty($surname)){
$where[]="Surname LIKE '%$surname%'";
}
$where = implode(' AND ', $where);
$sql="SELECT Name,Surname FROM TABLE WHERE $where ORDER BY Name DESC";
$res = mysql_query($sql);
while ($row = mysql_fetch_array($res, MYSQL_NUM)) {
$name = $row[0];
$surname = $row[1];
echo"$name";
echo"$surname";
}
如何在搜索“Name LIKE %surname%”后以 ASC 模式(ORDER BY Name ASC)显示姓名或姓氏,而无需再次执行 SQL 查询?例如,当我搜索包含“ich”字的名称时,我想以这种方式列出一个列表:
MICHAEL
PICHAEL
XICHAEL
=> the ASC mode
XICHAEL
PICHAEL
MICHAEL
=> the DESC mode