开发者

How to do LIKE search with PDO?

开发者 https://www.devze.com 2023-01-22 02:03 出处:网络
In order 开发者_StackOverflow中文版to do a LIKE search with PDO, I need to add the % to the parameter before passing it.

In order 开发者_StackOverflow中文版to do a LIKE search with PDO, I need to add the % to the parameter before passing it.

This works:

$qry = ' 
    SELECT product_id
    FROM cart_product
    WHERE product_manufacturer_num LIKE :search_string
';
$sth = $this->pdo->prepare($qry);
$sth->execute( array("search_string"=>'%'.$search_string.'%') );

To me this feels more like a hack, Is there a more official way of doing this?


It's fine. It doesn't feel like a hack to me.

The difficulty comes when you want to allow a literal % or _ character in the search string, without having it act as a wildcard.

0

精彩评论

暂无评论...
验证码 换一张
取 消