【问题标题】:CodeIgniter search results are unexpected; where() with or_like() broken?CodeIgniter 搜索结果出乎意料; where() 和 or_like() 坏了?
【发布时间】: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-&gt;db-&gt;or_like('column_name', $criteria);
  • @Samutz,是的,我写这个问题的错误。已编辑。 $criteria 是一个包含多个字段名称的数组。
  • @JohnB,很抱歉我写错了问题。 or_like() 正在查看三列。请参阅我编辑的问题。谢谢。
  • 如果你想结合 ORAND 条件,你可能无法使用 Active Record 构建查询......我假设你想要 WHERE (condition OR condition OR condition) AND date_condition 和什么你得到的是 WHERE condition OR condition OR condition AND date_condition 这是完全不同的

标签: mysql database codeigniter search


【解决方案1】:

首先,你为什么不使用 MySQL 的日期/时间函数:

$this->db->where('DATE(date) >= DATE(NOW())');

(您可能不需要 DATE(日期),而只需要日期)

但是,正如 cmets 中所述,您的查询可能不会按照您的意愿生成,就andor 而言。要测试输出查询的样子,请使用echo $this-&gt;db-&gt;last_query();。您始终可以使用自定义字符串编写 Active Records 查询的 where 部分,就像我上面的示例一样,这应该可以解决 and/or 问题。

所有这些都可以帮助您解决问题。

编辑

聊天结束后,这是最终的解决方案:

$where = "DATE(date) >= DATE(NOW()) 
          AND 
          (event LIKE '%$your_string%' 
          OR location LIKE '%$your_string%' 
          OR description LIKE '%$your_string%')";
$this->db->where($where);

【讨论】:

  • 我喜欢$this-&gt;db-&gt;where('DATE(date) &gt;= DATE(NOW())');,但它并没有解决根本问题。 echo $this-&gt;db-&gt;last_query() 也没有做任何事情。
  • 是的,MySQL 函数更适合用于... MySQL :) 而且速度更快...您需要将 echo 部分放在 AR 的 get 部分之后...不确定是否您的缓存会破坏它...如果是这样,请尝试在测试时将其关闭。
  • 是的,echo 是最后一个。如果我关闭缓存,那么结果就更不可预测了。
  • 我没有这方面的经验。你能提供一些指导吗?我正在尝试写出查询,它似乎忽略了LIKE 部分并返回了所有内容。
  • 哇,你能在这里写下你完整的 Active Records 部分(最终版本),以及echo 的输出吗?
【解决方案2】:

我在 ellislab 论坛 https://ellislab.com/forums/viewthread/185983/ 找到了答案 而不是使用

$this->db->like('location', $your_string);

使用:

$this->db->where('location LIKE', '%'.$your_string.'%');

or_where 而不是 or_like

【讨论】:

  • 因为您可以使用 Codeigniter 函数而不是接受的答案中建议的普通查询。
  • 接受的答案 is 使用 CI 函数 $this-&gt;db-&gt;where() 并且您的答案使用相同。
  • 在我看来,公认的答案是结合 CI 功能的经典查询。我在回答中只使用 CI 函数。反正我错了……
猜你喜欢
  • 2018-06-17
  • 2021-03-25
  • 1970-01-01
  • 2016-09-03
  • 1970-01-01
  • 2015-11-16
  • 2011-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多