对于这个功能企业上还算比较实用,推荐给大家;
index.
/p>
sea.php
$con = mysqli_connect("localhost","username","password");
if(!$con){
echo "数据库链接失败";
exit;
}
mysqli_select_db($con,'jwhuang');
mysqli_query($con,'set names utf-8');
$text= isset($_GET['text']) ? trim($_GET['text']) : '';
$result=mysqli_query($con,"select * from search where sea LIKE '{$text}%' ");
$search=array();
while($row=mysqli_fetch_assoc($result)){
//判断是否有对应的数据
if(!$row){
$search='';
exit;
}else{
//对查询关键字进行标记
$row['sea'] = str_replace($text, '' .$text. '', $row['sea']);
$search[]=$row;
}
}
echo json_encode($search);
?>
效果图
完整代码下载:https://github.com/jwhuang59/Demos/tree/master/search
以上这篇jq.ajax+php+mysql实现关键字模糊查询(示例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持前端开发者。