php模糊查询源码,PHP模糊查询技术实例分析【附源码下载】本⽂实例讲述了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='⽤户表'
索引的好处:
connect下载
如果按照某个条件去检索数据,如果这个条件字段没有建⽴索引,查询的时候是会遍历整张表,如果你建⽴了索引,查询的时候就会根据索引来查询,进⽽提⾼查询性能
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 '^...$';
案例
开发流程
源码分析
//关键字
$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, "".$keywords."",$row['username']); //⾼亮显⽰,不区分关键字的⼤⼩写
$usernamearr = preg_split('/(?
foreach ($usernamearr as $key => $value) {
if(strtoupper($keywords) == strtoupper($value)){
$usernamearr[$key] = "".$value."";
}
}
$row['username'] = join($usernamearr);
$usersarr[] = $row;
}
>
php⽤户查询器
php模糊查询
⽤户名:
if(!empty($keywords)){
echo "查询关键词: ".$keywords." 结果!";
}
$tablestring = "
$tablestring .= "
⽤户名邮箱性别";
if(!empty($usersarr)){
foreach ($usersarr as $key => $value) {
$tablestring .= "
" . $value['username']. "" . $value['email'] . "".$value['sex']."";
}
}else{

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。