资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

php数据库关键词搜索 php搜索框查询数据库

ThinkPHP关键字搜索(从MySQL数据库中)

提交的时候记得把默认的值去掉  才能判断是否有值..

成都创新互联公司2013年开创至今,是专业互联网技术服务公司,拥有项目做网站、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元聂荣做网站,已为上家服务,为聂荣各地企业和个人服务,联系电话:028-86922220

//这个是把三个搜索关键词作为独立的因子搜索

function search(){

if(isset($_POST['id'])  intval($_POST['id'])0){

$sql="select * from tbl  where id=".intval($_POST['id'])." ";

}

if(isset($_POST['name'])){

$sql.="union select * from tbl where name=".$_POST['name']." ";

}

if(isset($_POST['content'])){

$sql.="union select * from tbl where content like '%".$_POST['content']."%' ";

}

$s = M('search');

$result=$s-query($sql);

}

}

//以下是把三个搜索当作条件进行搜索  有筛选的味道

function search(){

$where="1=1";

if(isset($_POST['content'])){

$where.=" and content like '%$_POST[content]%'";

}

if(isset($_POST['content'])){

$where.=" and name = '$_POST[name ]'";

}

if(isset($_POST['id'])  intval($_POST['id'])0){

$where.=" and id= '$_POST[id]'";

}

if($where != '1=1'){

$sql="select * from tbl $where";

}else{

throw new Exception('没有输入搜索词');

}

$s = M('search');

$result=$s-query($sql);

}

}

PHP中怎么实现关键字搜索?

PHP要实现关键字查搜索,需要用到like关键字来组合查询条件

like具体实现方法如下:

例一:

1    $userForm=M('user');    

1    $where['name']=array('like','phpernote%');    

2    $userForm-where($where)-select();

这里的like查询即为:name like 'phpernote%'

例二:

1$where['name']=array('like',array('%phpernote%','%.com'),'OR');

这里的like查询即为:name like '%phpernote%' or name like '%.com'

例三:

1$where['name']=array(array('like','%a%'),array('like','%b%'),array('like','%c%'),'phpernote','or');

这里的like查询即为:(`name` LIKE '%a%') OR (`name` LIKE '%b%') OR (`name` LIKE '%c%') OR (`name` = 'phpernote')

例四:

1$where['_string']='(name like "%phpernote%")  OR (title like "%phpernote")'

这里的like查询即为:name like '%phpernote%' or title like '%phpernote'

php mysql like 实现多关键词搜索的方法

或者叫,分词检索数据库

$res

=

mysql_query("select

*

from

peter

where

id

like

'%中草药%'

and

'%6%'");

//这样写是报错的;

$res

=

mysql_query("select

*

from

peter

where

id

like

'%中草药%'

or

'%6%'");

//而这样写是正确的;奇怪~

$res

=

mysql_query("select

*

from

peter

where

id

like

'%中草药%'

and

id

like

'%6%'");

//这样写是正确的;

$res

=

mysql_query("select

*

from

peter

where

id

like

'%中草药%'

or

id

like

'%6%'");

//这样写都是正确的;

以上就是小编为大家带来的php

mysql

like

实现多关键词搜索的方法全部内容了,希望大家多多支持脚本之家~


分享文章:php数据库关键词搜索 php搜索框查询数据库
标题来源:http://cdkjz.cn/article/dooshic.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220