mysql 多条件查询问题

WBOY
Release: 2016-06-23 14:39:44
Original
1058 people have browsed it

"select * from subcatalog WHERE name LIKE '%$keywords%' and catalog = $catalog ORDER BY rank $rank limit $pages, $rows"


上面的语句,怎么可以做到当 $catalog 这个值为空的时候,可以搜索其他的数据,现在这里catalog = $catalog 为空的时候列表就没有东西出来了 


回复讨论(解决方案)

当 $catalog 为空时
 catalog = $catalog ORDER
就变成
 catalog =  ORDER
显然出现了语法错误

至少需要用引号将 $catalog 括起,写作 '$catalog'

不过这样还是有问题的:只能查询到 catalog 为空的记录
如果你不希望这样,应该在 $catalog 为空时,不写 catalog='$catalog'

把and catalog = $catalog 这个条件提出来单独写成一个变量,当$catalog为空的时候整个条件都为空。。。

思路这样子的,语法自己调整下:

if($catalog==''){	$tiaojian = '';}else{	$tiaojian =  "and catalog = $catalog";}$sql = "select * from subcatalog WHERE name LIKE '%$keywords%' $tiaojian  ORDER BY rank $rank limit $pages, $rows"
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template