MySQL 如何在 CodeIgniter 中使用 LIKE 查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3746009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use a LIKE query with CodeIgniter?
提问by DisgruntledGoat
I want to run a query like this:
我想运行这样的查询:
SELECT * FROM table WHERE field LIKE '%search_term%'
In CI you can bind parameters to queries, if you used field=?
but this does not work for field LIKE "%?%"
. From debugging output it seems the query used is field LIKE "%'search'%"
.
在 CI 中,您可以将参数绑定到查询,如果您使用过,field=?
但这不适用于field LIKE "%?%"
. 从调试输出看来,使用的查询是field LIKE "%'search'%"
.
Is there an alternative way to do searching in CodeIgniter?
有没有其他方法可以在 CodeIgniter 中进行搜索?
回答by Mark Byers
You can use this query:
您可以使用此查询:
SELECT * FROM table WHERE field LIKE ?
And bind with %search%
instead of search
.
并用%search%
代替search
.
You should be aware that this query will be slow in MySQL. You might want to look at free-text search instead (Lucene, Sphinx, or MySQL's built-in free-text search functions).
你应该知道这个查询在 MySQL 中会很慢。您可能想要查看自由文本搜索(Lucene、Sphinx 或 MySQL 的内置自由文本搜索功能)。
回答by Snap Mash
this is active record class for codeigniter
这是 codeigniter 的活动记录类
$this->db->select('*');
$this->db->like('columnname','both');
$query=$this->db->get("tablesname");
$result=$query->result_array();
if(count($result))
{
return $result;
}
else
{
return FALSE;
}
You should try this..I think its help you.. both means %columnname%, before means %columnname, after means columnname%
你应该试试这个..我认为它对你有帮助..两者都表示 %columnname%, before 表示 %columnname, after 表示 columnname%
回答by dulaj sanjaya
$search_term=$this->input->post('textboxName');
$search_term="%".$search_term."%";
$sql="SELECT * FROM table WHERE field LIKE ? ";
$query=$this->db->query($sql,array($search_term));
$res=$query->result();
回答by Sandy
what i can understand CI is adding quotes, pass FALSE as third parameter while binding to prevent CI adding quotes.
我能理解的 CI 是添加引号,在绑定时将 FALSE 作为第三个参数传递以防止 CI 添加引号。