【发布时间】:2013-04-19 22:22:13
【问题描述】:
今天的日期是 2013 年 4 月 25 日。是的,服务器上的时间是正确的,
mdate('%Y-%m-%d', strtotime(now()))的var_dump正在返回2013-04-25。date的 MySQL 表列是date类型/格式。CodeIgniter 2.1.3
我的搜索条件字符串是
"test"。
$criteria = array(
'field1' => 'test',
'field2' => 'test',
'field3' => 'test'
);
我的数据库中包含“test”一词的四个项目的日期如下:
date
2013-04-02
2013-04-05
2013-05-07
2013-05-21
在我的模型中,当我希望我的搜索结果 (search = "test") 无论日期如何都返回所有内容时,这就是它的样子...
$this->db->start_cache();
$this->db->or_like($criteria);
$this->db->order_by('date', 'desc');
$this->db->stop_cache();
$query['results'] = $this->db->get('table', $limit, $offset)->result_array();
$this->db->flush_cache();
return $query;
按预期输出:
2013-05-21
2013-05-07
2013-04-05
2013-04-02
以上工作完美,所有四个项目都返回了。
但是,当使用它并且仅添加 where() 以将结果优化到今天甚至更大时...
$this->db->start_cache();
$this->db->or_like($criteria); // <-- remove this line and it works
$this->db->where('date >=', mdate('%Y-%m-%d', strtotime(now()))); // <-- only new line
$this->db->order_by('date', 'asc');
$this->db->stop_cache();
$query['results'] = $this->db->get('table', $limit, $offset)->result_array();
$this->db->flush_cache();
return $query;
意外输出:
2013-04-02 // <-- why this one??? today is 4/25
2013-05-07
2013-05-21
它应该只显示今天和未来的结果。 那么为什么今天是 2013 年 4 月 25 日的项目日期为 2013 年 4 月 2 日?(这就像它部分工作,因为它仍然忽略 2013-04-05 的项目。)
- 为什么会这样?
- 如何解决这个问题?
编辑:
我试过颠倒顺序:
$this->db->where('date >=', mdate('%Y-%m-%d', strtotime(now())));
$this->db->or_like($criteria);
和链接:
$this->db->or_like($criteria)->where('date >=', mdate('%Y-%m-%d', strtotime(now())));
还有这个:
$this->db->or_like($criteria);
$this->db->where('DATE(date) >= DATE(NOW())');
结果是一样的。
【问题讨论】:
-
尝试使用 $this->db->last_query();看看究竟是什么查询代码点火器正在为初学者运行。我的猜测是 OR 就像创建一个一般的 OR 场景并附上 04/02 日期
-
不应该 or_like() 使用2个参数吗?
$this->db->or_like('column_name', $criteria); -
@Samutz,是的,我写这个问题的错误。已编辑。
$criteria是一个包含多个字段名称的数组。 -
@JohnB,很抱歉我写错了问题。
or_like()正在查看三列。请参阅我编辑的问题。谢谢。 -
如果你想结合
OR和AND条件,你可能无法使用 Active Record 构建查询......我假设你想要WHERE (condition OR condition OR condition) AND date_condition和什么你得到的是WHERE condition OR condition OR condition AND date_condition这是完全不同的
标签: mysql database codeigniter search