简体   繁体   中英

zend db select like wildcards

was trying to find some information about "like" clause in selects with zend db, but had no success. The problem is that it's not clear how to use the wildcard "%" near LIKE clause

The bad idea, which I've used initially and which is recommended in hundreds of places on the net is

 $db->select()
    ->where('text LIKE "%'.$query.'%"');

I guess, that the correct answer would be something like:

 $db->select()
    ->where('text LIKE ?', $query);

but there is no wildcards used

I've tried several solutions, but they all don't seem to work:

$db->select()
    ->where('text LIKE ?', '%$query%');
$db->select()
    ->where('text LIKE ?', '%{$query}%');

Could anyone please point to the documentation, or provide a good solution for this?

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.

doesn't tell enough

The % symbol needs to be part of the variable that is bound to the query. Your second example would work if you were using double quotes instead of single. So you can either do:

$query = '%'.$query.'%';
$db->select()
   ->where('text LIKE ?', $query);

or:

$db->select()
   ->where('text LIKE ?', "%$query%");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM