本篇文章给大家带来的内容是关于php如何实现模糊查询(图文代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
简介从本质上揭密php模糊查询技术
功能根据输入的关键字查找相关用户
php用户查询器案例分析课程目标掌握php模糊技术的应用
课程重点php模糊查询的语法
php模糊查询的应用
课程案例(效果图)
数据库设计
用户表(user):
create table user( `uid` int(10) auto_increment primary key comment '用户id', `username` varchar(30) not null default '' comment '用户名', `password` varchar(6) not null default '' comment '密码', `sex` char(2) not null default '保密' comment '性别', `email` varchar(40) not null default '' comment '邮箱', `hobby` varchar(255) not null default '' comment '兴趣爱好', key `username`(`username`)//索引)engine=myisam default charset=utf8 comment='用户表'
索引的好处:
如果按照某个条件去检索数据,如果这个条件字段没有建立索引,查询的时候是会遍历整张表,如果你建立了索引,查询的时候就会根据索引来查询,进而提高查询性能
mysql模糊查询语法sql匹配模式(开发中应用最多的一种)
正则表达式匹配模式
sql匹配模式使用sql匹配模式,不能使用操作符=或者!=,而是使用操作符like或者not like
使用sql匹配模式,mysql提供两种通配符:
①%表示任意数量的任意字符(其中包含0个)
②_表示的任意单个字符
使用sql匹配模式,如果匹配格式中不包含以上两种通配符的任意一个,其查询效果等同于=或者!=
使用sql匹配模式,默认情况下不区分大小写
代码实现:select * from user where username like 'l%';select * from user where username like '%e';select * from user where username like '%o%';select * from user where username like '___';//三个_,表示username为三个字符的结果集select * from user where username like '_o%';//第二个字符为o
正则表达式匹配模式. 匹配任意单个字符
* 匹配0个或多个在他前面的字符
eg:x* 表示匹配任何数量的x字符
[] 匹配括号中的任意字符
eg:[abc] 匹配字符a、b后者c [a-z] 匹配任何字母 [0-9] 匹配任何数字 [0-9]* 匹配任何数量的任何数字 [a-z]* 匹配任何数量的任何字母
^ 表示以某个字符或者字符串开头
eg:^a 表示以字母a开头
$ 表示已某个字符或者字符串结果
eg:s$ 表示以字母s结尾
使用正则表达式匹配模式使用的操作符:regexp(rlike) 或者not regexp(not rlike)
code:select * from user where username regexp '^l';select * from user where username regexp '...';
ps:如果仅使用.通配符,有几个点通配符,假设n个,那么匹配模式表示大于等于n个
精确字符数
^...$ //表示只能为三个字符select * from user where username regexp '^...$';
案例开发流程
源码分析<?php//关键字$keywords = isset($_post['keywords'])?$_post['keywords']:'';//连接数据库,php7废弃了mysql_connect推荐使用mysqli_connect$link = mysqli_connect( "localhost:3306", "root", "root", "mook");if(!empty($keywords)){ $sql = "select * from user where username like '%{$keywords}%' ";}else{ $sql = "select * from user";}$usersarr = [];$result = $link->query($sql);while($row = $result->fetch_assoc()){ //简单高亮显示 // $row['username'] = str_replace($keywords, "<font color='red'>".$keywords."</font>",$row['username']); //高亮显示,不区分关键字的大小写 $usernamearr = preg_split('/(?<!^)(?!$)/u',$row['username']); foreach ($usernamearr as $key => $value) { if(strtoupper($keywords) == strtoupper($value)){ $usernamearr[$key] = "<font color='red'>".$value."</font>"; } } $row['username'] = join($usernamearr); $usersarr[] = $row;}?><!doctype html><html><head> <meta charset="utf-8"> <title>php用户查询器</title></head><body> <h1>php模糊查询</h1> <form action="index.php" method="post"> 用户名:<input type="text" name="keywords" value="" /> <input type="submit" value="提交查询" /> </form> <?php if(!empty($keywords)){ echo "查询关键词:<font color='red'> ".$keywords." </font>结果!"; } $tablestring = "<table width='500' border='1' cellpadding='5'>"; $tablestring .= "<tr bgcolor='orange'><th>用户名</th><th>邮箱</th><th>性别</th></tr>"; if(!empty($usersarr)){ foreach ($usersarr as $key => $value) { $tablestring .= "<tr><td>" . $value['username']. "</td><td>" . $value['email'] . "</td><td>".$value['sex']."</td></tr>"; } }else{ $tablestring .="<tr><td colspan='3'>没有数据</td></tr>"; } $tablestring .= "</table>"; echo $tablestring; ?> </body></html>
源码下载地址:
链接:https://pan.baidu.com/s/1f8duu8g2ica4xgnlbbilcg 密码:96fn
相关推荐:
php实现会员账号只能唯一登录的代码实例
php实现会员账号只能唯一登录的代码实例
以上就是php如何实现模糊查询(图文代码)的详细内容。