本文介紹了如何在 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 子句?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!