1.概念


模糊查询原理,本质上是对sql语句的查询操作;使用sql匹配模式,只能使用操作符LINK或者NOT LINK;并且提供了两种通配符%表示任意数量的任意字符(其中包括0个)以及_表示的任意单个字符;如果匹配格中不包含以上两种通配符的任意一个,其中查询的效果等同于=或者!=

 

2.案例




2.1查询以某个字符开头的用户




查询以符号l开头的用户

select*from user where username like 'l%';

 

2.2查询以某个字符结尾的用户




查询符号e结尾的用户

select*from user where username like '%e';

 

2.3查询包含某个字符的用户




查询用户名包含字符'0'的用户

select*from user where username like '%0%';

 

2.4查询与限定用户长度匹配的用户




查询用户长度为3的用户

select*from user where username like '___';

 

2.5两种通配符的结合使用




 

查询用户名第二个字符为o的用户

select*from user where username like '_O%';

 

2.6总结




模糊查询语句就是使用sql语句中的LIKE语句进行查询。

 

 

3. 具体案例代码




3.1具体实现代码

<?php
$keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';
$conn = @mysql_connect("localhost", "root", "root") or die("数据库链接错误");
mysql_select_db("sql", $conn);
mysql_query("set names 'utf8'"); //使用utf-8中文编码;
//PHP模糊查询
$sql="SELECT * FROM user WHERE username LIKE '%{$keywords}%'";
$rs= mysql_query($sql);
$users = array();//保存所以得查询到的用户
if(!empty($keywords)){
 while ($row=mysql_fetch_assoc($rs)){
 $users[] = $row;
 }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
查询器</title>
 <style>
 .textbox {
 width: 355px;
 height: 40px;
 border-radius: 3px;
 border: 1px solid #e2b709;
 padding-left: 10px;
 }
 .su {
 width: 365px;
 height: 40px;
 background-color: #7fbdf0;
 color: white;
 border: 1px solid #666666;
 }
 table{ background-color: #7fbdf0; line-height:25px;}
 th{ background-color:#fff;}
 td{ background-color:#fff; text-align:center}
 </style>
</head>
<body >
<form action="" method="get">
请输入内容"/>
查询"/>
</form>
<?php
if ($keywords){
查询关键词:<font color="red">'.$keywords.'</font></h3>';
}
if ($users){
 echo '<table width="500" cellpadding="5" >';
用户名</th><th>密码</th><th>邮箱</th><th>性别</th><th>爱好</th>';
 foreach ($users as $key=>$value){
 echo '<tr>';
 echo '<td>'.$value['username'].'</td>';
 echo '<td>'.$value['password'].'</td>';
 echo '<td>'.$value['sex'].'</td>';
 echo '<td>'.$value['email'].'</td>';
 echo '<td>'.$value['hobby'].'</td>';
 echo '</tr>';
 }
}else{
没有查询到相关用户';
}
?>
</body>
</html>

 

3.2查询搜素之关键词变色

上一节我们已经基本完成了如何搜索关键词,我们接下来对搜索显示做美化。

在模糊查询语句中加入下面你的代码

$row['username']=str_replace($keywords,'<font color="red">'.$keywords.'</font>',$row['username']);

他的意思是把查询的名字中查询的关键词替换成红色的字体。

 

完整代码:

<?php
$keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : '';
$conn = @mysql_connect("localhost", "root", "root") or die("数据库链接错误");
mysql_select_db("sql", $conn);
mysql_query("set names 'utf8'"); //使用utf-8中文编码;
//PHP模糊查询
$sql="SELECT * FROM user WHERE username LIKE '%{$keywords}%'";
$rs= mysql_query($sql);
$users = array();//保存所以得查询到的用户
if(!empty($keywords)){
 while ($row=mysql_fetch_assoc($rs)){
 $row['username'] = str_replace($keywords,'<font color="red">'.$keywords.'</font>',$row['username']);
 $users[] = $row;
 }
}
?>
<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
查询器</title>
 <style>
 .textbox {
 width: 355px;
 height: 40px;
 border-radius: 3px;
 border: 1px solid #e2b709;
 padding-left: 10px;
 }
 .su {
 width: 365px;
 height: 40px;
 background-color: #7fbdf0;
 color: white;
 border: 1px solid #666666;
 }
 table{ background-color: #7fbdf0; line-height:25px;}
 th{ background-color:#fff;}
 td{ background-color:#fff; text-align:center}
 </style>
</head>
<body >
<form action="" method="get">
请输入内容"/>
查询"/>
</form>
<?php
if ($keywords){
查询关键词:<font color="red">'.$keywords.'</font></h3>';
}
if ($users){
 echo '<table width="500" cellpadding="5" >';
用户名</th><th>密码</th><th>邮箱</th><th>性别</th><th>爱好</th>';
 foreach ($users as $key=>$value){
 echo '<tr>';
 echo '<td>'.$value['username'].'</td>';
 echo '<td>'.$value['password'].'</td>';
 echo '<td>'.$value['sex'].'</td>';
 echo '<td>'.$value['email'].'</td>';
 echo '<td>'.$value['hobby'].'</td>';
 echo '</tr>';
 }
}else{
没有查询到相关用户';
}
?>
</body>
</html>