>这是代码 。
我想搜索数据库中的每个单词,但结果只显示单个关键字而不是多个。 它是一个PDO数据库 当我搜索 facebook 这样的单词时,会显示结果 如果我搜索两个或多个单词搜索,则结果不会出现。
'
function getResults(){
$q=$GLOBALS['q'];
$p=$GLOBALS['p'];
$start=($p-1)*10;
if($p!=null){
$starttime = microtime(true);
$sql=$GLOBALS['dbh']->prepare('SELECT title, url, description FROM search WHERE `title` LIKE :q OR `url` LIKE :q OR `description` LIKE :q ');
$sql->bindValue(":q", "%$q%");
$sql->execute();
$trs=$sql->fetchAll(PDO::FETCH_ASSOC);
$endtime = microtime(true);
if($sql->rowCount()==0 || $start>$sql->rowCount()){
return 0;
}else{
$duration = $endtime - $starttime;
$res=array();
$res['count']=$sql->rowCount();
$res['time']=round($duration, 4);
$limitedResults=array_slice($trs, $start, 12);
foreach($limitedResults as $r){
$res["results"][]=array($r['title'], $r['url'], $r['description']);
}
return $res;
}
}
}
?>
'
答案 0 :(得分:1)
不确定为什么你没有使用@david strachan的方法(我认为这是一个非常好的解决方案),但我会尝试解释你也可以使用的一个选项并理解为什么你&# 39;目前没有得到任何结果。
此时发生的情况是,您将这些值发送到您的查询中,最终成为;
SELECT title, url, description FROM search WHERE `title` LIKE `google+facebook+yahoo` OR `url` LIKE `google+facebook+yahoo` OR `description` LIKE `google+facebook+yahoo`
我不认为你想要找到包含所有这些的小东西。
我不确定如何使用查询来编写此内容,但您可以使用快速解决方案。
答案 1 :(得分:0)
您必须构建动态查询才能搜索两个单词。在下面的代码中,我使用GET来说明构建查询以显示查询和数组。我尽可能使用未命名的参数?
和“延迟”绑定 - 将数据传递给执行以方便。
//Using GET here for testing
<?php
//Using GET here for testing
if(isset($_GET['q'])){
$q = $_GET['q'];
//}
//First split string
if (strpos($q, ' ') > 0) {
$pieces = explode(" ", $q);
//echo count($pieces);
//You need an array to store columns
$columnArray = array( "title", "url", "description");
//You need an array to store parameters
$paramArray =array();
$clause = "WHERE";
}
//You need to build a dynamic query to perform this. Start with a basic stub
$sql = "SELECT `title`, `url`, `description` FROM search ";
//Start building the query
//Also see PDO WIKI for use % in placeholders.ie placeholders cannot represent an arbitrary part of the query, but a complete data literal only
//Loop through columns
foreach ($columnArray as $column) {
//$sql .
foreach ($pieces as $value) {
$sql .= " $clause $column LIKE ?";
$clause = "OR";
array_push($paramArray,"%$value%");
}
}
//echo query and parameter array
echo $sql;
echo "<br>";
print_r($paramArray);
//Prepare and execute query
// $sth = $db->prepare($sql);
// $sth->execute($paramArray);
}
?>
结果来自 xxx.php?q = google amazon quora 或者xxx.php?q = google + amazon + quora
SELECT `title`, `url`, `description` FROM search WHERE title LIKE ? OR title LIKE ? OR title LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR description LIKE ? OR description LIKE ? OR description LIKE ?
Array ( [0] => %google% [1] => %amazon% [2] => %quora% [3] => %google% [4] => %amazon% [5] => %quora% [6] => %google% [7] => %amazon% [8] => %quora% )
答案 2 :(得分:-1)
这里是您发现有用的完整脚本。
<?php
if(isset($_GET["search"]))
{
$condition = '';
//$query = explode(" ", $_GET["search"]);
$query = explode(" ", $_GET["search"]);
foreach($query as $text)
{
$condition .= "`title` LIKE +'%".mysqli_real_escape_string($connect, $text)."%' OR ";
}
$condition = substr($condition, 0, -4);
$sql_query = "SELECT * FROM countries2 WHERE " . $condition;
$result = mysqli_query($connect, $sql_query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
echo '<tr><td>'.$row["title"].'</td></tr>';
}
}
else
{
echo '<label>Data not Found</label>';
}
}
?>