我试图在zend db的选择中找到一些关于“like”子句的信息,但没有成功.
问题是如何在LIKE子句附近使用通配符“%”并不清楚
问题是如何在LIKE子句附近使用通配符“%”并不清楚
我最初使用过的坏主意是在网上的数百个地方推荐的
$db->select() ->where('text LIKE "%'.$query.'%"');
我想,正确答案是这样的:
$db->select() ->where('text LIKE ?',$query);
但是没有使用通配符
我尝试了几种解决方案,但它们似乎都不起作用:
$db->select() ->where('text LIKE ?','%$query%'); $db->select() ->where('text LIKE ?','%{$query}%');
The second argument to the where() method is optional. It is a value to substitute into the expression. Zend_Db_Select quotes the value and substitutes it for a question-mark (“?”) symbol in the expression.
说不够
解决方法
%符号需要是绑定到查询的变量的一部分.如果您使用双引号而不是单引号,那么您的第二个示例将起作用.所以你可以这样做:
$query = '%'.$query.'%'; $db->select() ->where('text LIKE ?',$query);
要么:
$db->select() ->where('text LIKE ?',"%$query%");