我正在为我的网站创建一个搜索引擎,我想添加找到的结果数量,我寻找功能,但我发现的所有内容都不在PDO中。所以我决定两次运行查询,但我不确定它是否可行。有没有更好的方法来计算PDO中的结果?否则,两次运行相同的查询是否安全?谢谢!
<?php
if(isset($_GET['tab']))
{
if($_GET['tab'] == 'all')
{
$query = $db->query('SELECT * FROM search WHERE title LIKE \'%'.$_GET['searchquery'].'%\' OR details LIKE \'%'.$_GET['searchquery'].'%\' OR link LIKE \'%'.$_GET['searchquery'].'%\' LIMIT 20');
}
else if($_GET['tab'] == 'products')
{
$query = $db->query('SELECT * FROM search WHERE (type = \'products\') AND (title LIKE \'%'.$_GET['searchquery'].'%\' OR details LIKE \'%'.$_GET['searchquery'].'%\' OR link LIKE \'%'.$_GET['searchquery'].'%\') LIMIT 20');
}
else if($_GET['tab'] == 'blogpost')
{
$query = $db->query('SELECT * FROM search WHERE (type = \'blogpost\') AND (title LIKE \'%'.$_GET['searchquery'].'%\' OR details LIKE \'%'.$_GET['searchquery'].'%\' OR link LIKE \'%'.$_GET['searchquery'].'%\') LIMIT 20');
}
else if($_GET['tab'] == 'forumthread')
{
$query = $db->query('SELECT * FROM search WHERE (type = \'forumthread\') AND (title LIKE \'%'.$_GET['searchquery'].'%\' OR details LIKE \'%'.$_GET['searchquery'].'%\' OR link LIKE \'%'.$_GET['searchquery'].'%\') LIMIT 20');
}
?>
<div id="resultsconainter">
<div id="tabsconatiner">
</div>
<?php
else
{
echo '<p>Sorry, this section is not available!</p>';
}
if(isset($query))
{
for($i=0; $i<100; $i++)
{
if($result = $query->fetch())
{
$_GET['searchquery'] = rawurlencode($_GET['searchquery']);
$result = preg_replace("#\\b(" . $_GET['searchquery'] . ")\\b#i", "<b>$1</b>", $result);
echo '<div class="result">
<a class="title" href="/qsoft/'.$result['link'].'">'.$result['title'].'</a>
<span class="link">
<span class="beforelink" style="font-size:1.1em;font-size:1.3vw;position:relative;top:1px;padding-right:1px;">‣</span>
localhost/QSoft/'.$result['link'].'
</span>
<span class="details">'.$result['details'].'</span>
</div>';
}
else
{
if($i==0)
{
echo '<p>Sorry, no resluts found here for : <b>'.$_GET['searchquery'].'</b></p>';
}
}
}
}
}
else
{
echo'<p>Missing tab informations, please retry</p>';
}
}
?>
答案 0 :(得分:2)
是的,对于搜索引擎,您应该运行两个查询:
但是,对于mysql,您可以使用技巧
对于第一个查询,请在SQL_CALC_FOUND_ROWS
之后添加SELECT
关键字:
$query = $db->query('SELECT SQL_CALC_FOUND_ROWS * FROM search ... LIMIT 20');
而第二次只是运行这个简单的代码
$count = $db->query("SELECT FOUND_ROWS()")->fetchColumn();
答案 1 :(得分:-2)
假设您使用类似$result = $sth->fetchAll();
之类的内容从查询中获取所有记录,您可以执行count($result)
以返回多个记录。所以不需要1个查询
要获得更准确的答案,最好向我们提供部分代码,以便我们为您提供帮助。
答案 2 :(得分:-2)
好的,所以这就是我所做的,我不是很干净但它有效:
if(isset($query))
{
$didgetcount = false;
for($i=0; $i<100; $i++)
{
if($result = $query->fetch())
{
$_GET['searchquery'] = rawurlencode($_GET['searchquery']);
$result = preg_replace("#\\b(" . $_GET['searchquery'] . ")\\b#i", "<b>$1</b>", $result);
echo '<div class="result">
<a class="title" href="/qsoft/'.$result['link'].'">'.$result['title'].'</a>
<span class="link">
<span class="beforelink" style="font-size:1.1em;font-size:1.3vw;position:relative;top:1px;padding-right:1px;">‣</span>
localhost/QSoft/'.$result['link'].'
</span>
<span class="details">'.$result['details'].'</span>
</div>';
}
else
{
if($i==0)
{
echo '<p>Sorry, no resluts found here for : <b>'.$_GET['searchquery'].'</b></p>';
echo '<script> document.getElementById("resultsnumber").innerText = 0;</script>';
$didgetcount = true;
}
else
{
if($didgetcount == false)
{
echo '<script>
function writeresults(n)
{
document.getElementById("resultsnumber").innerText = n;
}
writeresults('.$i.');
</script>';
$didgetcount = true;
}
}
}
}
}
}
else
{
echo'<p>Missing tab informations, please retry</p>';
}