如何在 PDO 准备语句中使用 LIKE 子句? [英] How do I use a LIKE clause in a PDO prepared statement?

查看:36
本文介绍了如何在 PDO 准备语句中使用 LIKE 子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的 sql 查询:

I have a sql query like this:

SELECT * FROM tbl_name WHERE title Like "%:needle%"

当我使用此语句手动查询 MySQL 数据库时,它可以工作.但是,当我将它与 PDO 一起使用并使用与我手动查询相同的 :needle 值时,它只返回一个空结果集.

When I query the MySQL db manually with this statement it works. But when I use it with PDO and with the same values for :needle as I queried manually It just returns an empty result set.

utf8 编码会影响它的行为吗?

Does utf8 encoding affects the behavior of it?

推荐答案

使用 PDO,可以这样做:

With PDO, this can be done like:

$stmt = $db->prepare("SELECT * FROM tbl_name WHERE title LIKE :needle");
$needle = '%somestring%';
$stmt->bindValue(':needle', $needle, PDO::PARAM_STR);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

这篇关于如何在 PDO 准备语句中使用 LIKE 子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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