我试图创建一个带有日期范围过滤器的搜索引擎,但是每当我在“ search_textbox”上键入内容时,它都不会包含日期,它只会搜索包含“%a”等语句“ name”的字段';
if($search !=''):
$added_query = "and date_created like '%".$search."%' or
name like '%".$search."%' or
alias like '%".$search."%' or
designation like '%".$search."%'
";
else:
$added_query ="";
endif;
$project_details = $this->db->query("SELECT *
FROM ".$query."_man_power
WHERE date_created BETWEEN '".$date_from."' AND '".$date_to."'
".$added_query."
order by date_created desc
");
return $project_details;
答案 0 :(得分:0)
您可能没有正确格式化where子句中的日期。看到: https://www.w3schools.com/SQl/sql_dates.asp
根据类型,您要确保$date_from
和$date_to
的外观如下:
$date_from = '2018-06-01 0:00:00';
$date_to = '2018-06-30 23:59:59';
您可以通过查看查询来更好地检查查询以发现问题。
$sql = "SELECT * FROM ".$query."_man_power";
$sql .= " WHERE date_created BETWEEN '".$date_from."' AND '".$date_to."'".$added_query;
$sql .= " order by date_created desc";
var_dump($sql);
$project_details = $this->db->query($sql);
答案 1 :(得分:0)
在组合这些条件时,重要的是使用括号,以便数据库知道对每个条件求值的顺序。因此,将AND,OR条件与括号分组,例如:-
if($search !=''):
$added_query = "and (
date_created like '%".$search."%' or
name like '%".$search."%' or
alias like '%".$search."%' or
designation like '%".$search."%'
)";
else:
$added_query ="";
endif;
$project_details = $this->db->query("SELECT *
FROM ".$query."_man_power
WHERE date_created BETWEEN '".$date_from."' AND '"
.$date_to."'".$added_query." order by date_created desc");
return $project_details;`