我有这段代码可以从我的数据库返回搜索结果:
$search_stmt = $dbConnection->prepare("SELECT * from `books` WHERE `title` LIKE '%testing1234%' ORDER BY `rating` DESC, `title` ASC LIMIT 1000");
这是我的数据库的一部分:
---------------------
| id |title |
---------------------
| 1 |testing1234 |
---------------------
| 2 |Testament... |
---------------------
这不会从我的表中返回任何内容。
将 testing1234
替换为 testing
,这会返回行 1
。
我还尝试将 testing1234
替换为 test
,这会返回两行。
我还用 REGEXP 'testing1234'
替换了 LIKE '%testing1234%'
,但使用 REGEXP
得到的结果与我得到了所有 LIKE
语句。
当我将原始查询放入 Sequel Pro 中时,它返回行 1
。
所以我的结论是我没有正确使用 PDO,任何帮助我朝着正确方向前进的帮助将不胜感激!
最佳答案
答案是确保在建立与数据库的连接时定义了字符集
。
这是连接的原始代码:
$dbConnection = new PDO('mysql:host=' . $db_host . ';dbname=' . $db_name . '', $db_user, $db_pass);
应该这样做:
$dbConnection = new PDO('mysql:host=' . $db_host . ';dbname=' . $db_name . ';charset=utf8', $db_user, $db_pass);
关于php - LIKE 语句在 PDO 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29635521/