我正尝试为我的网站创建一个搜索引擎,用户可以在其中输入关键字,PHP脚本将搜索'name'和'description'我的fyp_items表的列。到目前为止,我已经设法将输入分解为一个单词数组,并尝试在我的数据库上执行SELECT查询。PHP搜索引擎即使输入与表中的数据匹配,也不会返回任何结果
问题是即使关键字与表中数据的关键字匹配,它也无法找到项目。下面是我的剧本......我希望有人能帮助我。
if(empty($_POST)=== false){
$output = '';
$error = '';
$input = $_POST['search_input'];
$i=0;
if($input){
$keyword = explode(" ", $input);
require ('core/dbconnection.php');
//If a user is logged in check if the user is Admin or Customer.
if(isset($_SESSION['userid'])){
if($admin == 1){
}
}else{
//If user is not logged in search items table only.
$search_items = "SELECT * FROM fyp_items WHERE ";
foreach($keyword as $k){
$k = mysql_real_escape_string($k);
$i++;
if($i == 1){
$search_items .= "name LIKE '$k' OR description LIKE '$k'";
}else
$search_items .= " OR name LIKE '$k' OR description LIKE '$k'";
}
$item_qry = mysql_query("$search_items")or die(mysql_error());
$numrows = mysql_num_rows($item_qry);
if($numrows > 0){
$output = 'found it';
}else
$error = '<p class="pageerror">Sorry, what you were looking for was not found.</p>';
}
}else
$error = '<p class="pageerror">Please enter your search terms.</p>';
我已经测试通过回输出的帖子,我也呼应$search_items
变量来得到这个... http://awesomescreenshot.com/05c2fwytac
您的帮助将非常感激!
尝试使用'LIKE'%word%''使用wilcard –
是的,%word%是我用于MySQLi的,它适用于我。尝试一下。 – 2014-03-04 20:25:12
你可能会考虑使用MySQL自然语言搜索。 –