Home > Backend Development > PHP Tutorial > thinkphp数组查询问题

thinkphp数组查询问题

WBOY
Release: 2016-06-23 13:26:16
Original
990 people have browsed it

thinkphp中查询条件为

$map['id'] = I('get.id');$map['cid'] = i('get.cid');$m->where($map)->find();
Copy after login


生成的SQL语句为
SELECT `id`,`title`,`pic` FROM `news` WHERE `id` = null AND `cid` = null  LIMIT 0,12
Copy after login


也就是说,当获取的ID和CID为空时,这个查询条件还是存在的,但值变成了null,这样查不到数据,
正确的应该是当获取的值为空时,where条件就应该不存在了,就是搜索全部的信息了,
请问这个什么原因呢?


回复讨论(解决方案)

从不用 thinkphp,不过不止一次见到类似的问题,估计是他的一个 bug
如果没有能力修改他的代码,可以先预处理一下

$map['id'] = I('get.id');$map['cid'] = i('get.cid');$map = array_diff($map, array('')); //过滤掉空元素$m->where($map)->find();
Copy after login

 嗯,确实是一个BUG,我用循环把这个去掉了,但代码太多,用这个函数方便 多了

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