查询的基本原理

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>查询器</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">

 <p><input type="text" name="keywords" class="textbox" value="" placeholder="请输入内容"/>

 <p><input type="submit" class="su" value="查询"/>

</form>

<?php

if ($keywords){

 echo '<h3>查询关键词:<font color="red">'.$keywords.'</font></h3>';

}

if ($users){

 echo '<table width="500" cellpadding="5" >';

 echo '<tr><th>用户名</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{

 echo '没有查询到相关用户';

}

?>

</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>查询器</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">

 <p><input type="text" name="keywords" value="" placeholder="请输入内容"/>

 <p><input type="submit" value="查询"/>

</form>

<?php

if ($keywords){

 echo '<h3>查询关键词:<font color="red">'.$keywords.'</font></h3>';

}

if ($users){

 echo '<table width="500" cellpadding="5" >';

 echo '<tr><th>用户名</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{

 echo '没有查询到相关用户';

}

?>

</body>

</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值