LIKE查询无法运行PDO

时间:2014-10-21 07:23:24

标签: php mysql pdo

LIKE查询不起作用, 它只输出空白。

enter image description here

$stmt = $dbo->prepare("SELECT * FROM hotels WHERE h_country=:c LIKE '%m%'");
$stmt->bindValue(":c", "malaysia", PDO::PARAM_STR);
$stmt->execute();

2 个答案:

答案 0 :(得分:1)

你的SQL语法错了,不确定你想要获得什么,但是正确的是:

SELECT * FROM hotels WHERE h_country LIKE '%' || :c || '%';


--OR

SELECT * FROM hotels WHERE h_country  = :c  AND <your_column_here> LIKE '%m%';

答案 1 :(得分:0)

更改

$stmt = $dbo->prepare("SELECT * FROM hotels WHERE h_country=:c LIKE '%m%'");

$stmt = $dbo->prepare("SELECT * FROM hotels WHERE h_country LIKE '%m%'");

您要么将值与LIKE函数进行比较,要么与变量&#39;:c&#39;进行比较。它的一个或另一个。

如果您想同时使用以下内容:

$stmt = $dbo->prepare("SELECT * FROM hotels WHERE h_country=:c OR  h_country LIKE '%m%'");