我有这个代码来搜索数据库。它现在完美无缺,但我想改变它显示的顺序。以下是我的代码。
if (isset($_GET["mainSearch"]))
{
$condition = '';
$query = explode(" ", $_GET["mainSearch"]);
foreach ($query as $text)
{
$condition .= "question LIKE '%".SQLite3::escapeString($text)."%' OR ";
}
$condition = substr($condition, 0, -4);
$order = "ORDER BY quiz_id DESC";
$sql_query = "SELECT * FROM questions WHERE " . $condition;
$sql_query_count = "SELECT COUNT(*) as count FROM questions WHERE " . $condition . $order;
$result = $db->query($sql_query);
$resultCount = $db->querySingle($sql_query_count);
if ($resultCount > 0)
{
if ($result)
{
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
echo '<div class="quesbox_3">
<div class="questitle">
<h2>'.$row["question"].'</h2>
</div>
<div class="quesanswer">'.$row["answer"].'</div>
</div>';
}
}
}
else
{
echo "No results found";
}
}
我希望结果显示的ORDER从下到上。请问如何正确添加此功能,因为当前代码无效。
答案 0 :(得分:1)
您将订单放在count()查询上,这实际上没什么用。
$sql_query = "SELECT * FROM questions WHERE " . $condition;
$sql_query_count = "SELECT COUNT(*) as count FROM questions WHERE " . $condition . $order;
尝试
$sql_query = "SELECT * FROM questions WHERE " . $condition. ' '. $order;
$sql_query_count = "SELECT COUNT(*) as count FROM questions WHERE " . $condition ;
答案 1 :(得分:0)
在此字符串中添加1个空格
$order = " ORDER BY quiz_id DESC ";
它没有显示你,因为如果你回显你的查询而不是像这样显示
SELECT COUNT(*) as count FROM questions WHERE question LIKE '%test%'ORDER BY quiz_id DESC
您可以在此回显查询中看到ORDER and %test%
这样做会对你有帮助。
答案 2 :(得分:0)
查询空间问题。你可以尝试这个查询
$sql_query_count = "SELECT COUNT(*) as count FROM questions WHERE " . $condition .' '. $order;
答案 3 :(得分:0)
您的$order
仅在您的计数查询中。添加到您的$sql_query
$sql_query = "SELECT * FROM questions WHERE " . $condition . ' '. $order;