如何使用PDO进行搜索? [英] How to do LIKE search with PDO?

查看:73
本文介绍了如何使用PDO进行搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了使用PDO进行LIKE搜索,我需要在传递参数之前将%添加到参数中.

In order to do a LIKE search with PDO, I need to add the % to the parameter before passing it.

这有效:

$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.

这篇关于如何使用PDO进行搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆