我正在尝试使用此脚本一次从数据库中搜索多个单词。现在的问题是我的脚本只搜索一个单词。但是,如果数据库包含这些单词,我想一次搜索更多单词(快速主页jquery),然后它将显示结果。我怎么能用这个脚本做任何身体告诉我的?或者我如何搜索任何关键字?请帮帮我......
<?php
include_once ('database_connection.php');
if(isset($_GET['keyword'])){
$keyword = trim($_GET['keyword']) ;
$keyword = mysqli_real_escape_string($dbc, $keyword);
$query = "select topictitle,topicdescription from topics where topictitle like '%$keyword%' or topicdescription like '%$keyword%'";
//echo $query;
$result = mysqli_query($dbc,$query);
if($result){
if(mysqli_affected_rows($dbc)!=0){
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo '<p> <b>'.$row['topictitle'].'</b> '.$row['topicdescription'].'</p>' ;
}
}else {
echo 'No Results for :"'.$_GET['keyword'].'"';
}
}
}else {
echo 'Parameter Missing';
}
?>
答案 0 :(得分:0)
分解搜索词,这样你就可以得到一个包含每个单词的数组并构建这样的查询?这个问题有点令人困惑,可能这样的事情对你有用:
if(isset($_GET['keyword'])){
$keyword = trim($_GET['keyword']);
$keyword = mysqli_real_escape_string($dbc, $keyword);
$search_terms = explode(" ",$keyword);
$query = "select topictitle,topicdescription from topics where 1 ";
foreach($search_terms as $searchterm){
$query = $query."or topictitle like '%".$searchterm."%' or topicdescription like '%".$searchterm." ';
}
...