我想按名称在 mysql 中搜索。
我在搜索模型中有此代码
$this->db->limit($limit, $start);
$this->db->like("name",$by);
$res = $this->db->get('walls');
if ($res->num_rows() > 0)
return $res->result();
$by = "megan";
的结果有效,但当$by = "megan fox";
不起作用时。
我的表中有一行名称列值为“megan Fox”;
我尝试了
urldecode()
但没有成功。
$this->db->limit($limit, $start);
$this->db->like("name",$by);
$res = $this->db->get('walls');
if ($res->num_rows() > 0)
return $res->result();
您可能需要在
BOTH
子句示例中添加 LEFT
、RIGHT
、like()
$this->db->like("name",$by,'BOTH'); // left and right wild card %name%
$this->db->like("name",$by,'LEFT'); // left wild card %name
$this->db->like("name",$by,'RIGHT'); // right wild card name%
您可以在 http://ellislab.com/codeigniter/user-guide/database/active_record.html
阅读更多内容您可以首先将
$by
变量拆分为单独的值,如下所示:
$by = explode(" ", $by);
然后尝试像这样制作like子句
$this->db->like("name",$by[0]);
$this->db->or_like("name",$by[1]);
它会产生这样的东西
WHERE name LIKE '%megan%' OR name LIKE '%fox%'
这假设您始终将 2 个变量传递给
$by
,并用空格分隔。
您必须调整它以确保它在每种情况下都有效,例如,当只有一个变量传递给 $by
时,您应该对其进行检查。
请注意,这比将姓名和姓氏拆分为表中两个单独的字段并在每个字段中查询特定的姓名要慢得多。如果您关心优化,您应该这样做。
使用urldecode函数来解决这个问题 在你的模型中尝试这个
$by= urldecode($by);
$this->db->limit($limit, $start);
$this->db->like("name",$by);
$res = $this->db->get('walls');
if ($res->num_rows() > 0)
return $res->result();