
正文
php查找数据库关键字 php查询sqlserver数据库
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
ThinkPHP关键字搜索(从MySQL数据库中)
提交php查找数据库关键字的时候记得把默认php查找数据库关键字的值去掉 才能判断是否有值..
//这个是把三个搜索关键词作为独立的因子搜索
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);
}
}
相关问答
Q1: PHP中怎么实现关键字搜索?
PHP要实现关键字查搜索php查找数据库关键字,需要用到like关键字来组合查询条件
like具体实现方法如下php查找数据库关键字:
例一:
1 $userForm=M('user');
1 $where['name']=array('like','phpernote%');
2 $userForm-where($where)-select();
这里php查找数据库关键字的like查询即为:name like 'phpernote%'
例二:
1$where['name']=array('like',array('%phpernote%','%.com'),'OR');
这里php查找数据库关键字的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'
Q2: php 查询 sql 数据库 提取文章关键词
$res=mysql_query("select
*
from
keyword");//查找所有关键词
while($row
=
mysql_fetch_array($rec)){//一次判断,文章中有没有出现该关键词
$str
=
$row['name'];
$rec
=
mysql_query("select
*
from
文章表
where
$title
like
'%$str%'
or
$content
like
'%$str%'");//模糊查询文章标题和内容中出现了的该关键词的数据
$num
=
mysql_num_rows($rec);//文章标题和内容中出现了的该关键词的文章条数
if($num){
echo
$str;
}
}
php查找数据库关键字的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php查询sqlserver数据库、php查找数据库关键字的信息别忘了在本站进行查找喔。






