PDO:绑定变量数与令牌数不匹配

时间:2014-06-11 05:43:29

标签: php pdo

PHP / SQL代码:

$sth = $dbh->prepare("SELECT * 
FROM  `jobs` 
WHERE  `city` = :city
AND  `district` = :district
AND  `type` = :type
AND (
CONVERT(  `id` 
USING utf8 ) LIKE  '% :keyword %'
OR CONVERT(  `owner` 
USING utf8 ) LIKE  '% :keyword %'
OR CONVERT(  `title` 
USING utf8 ) LIKE  '% :keyword %'
OR CONVERT(  `city` 
USING utf8 ) LIKE  '% :keyword %'
OR CONVERT(  `district` 
USING utf8 ) LIKE  '% :keyword %'
OR CONVERT(  `type` 
USING utf8 ) LIKE  '% :keyword %'
OR CONVERT(  `payrate` 
USING utf8 ) LIKE  '% :keyword %'
OR CONVERT(  `hours` 
USING utf8 ) LIKE  '% :keyword %'
OR CONVERT(  `description` 
USING utf8 ) LIKE  '% :keyword %'
)
LIMIT 0 , 30");

$sth->bindParam(':city', $city);
$sth->bindParam(':district', $district);
$sth->bindParam(':type', $type);
$sth->bindParam(':keyword', $keyword);
$sth->execute();

似乎是我的一个令牌/变量没有“绑定”得当吗?任何想法在哪里/如何纠正?

我猜我需要在某个地方逃避某些变量才能正常工作,但我对PDO及其布局的方式相当新鲜。

感谢。

1 个答案:

答案 0 :(得分:0)

尝试删除:keyword周围的引号,并将%通配符移至bindParam来电。

$sth = $dbh->prepare("SELECT * 
FROM  `jobs` 
WHERE  `city` = :city
AND  `district` = :district
AND  `type` = :type
AND (
CONVERT(  `id` 
USING utf8 ) LIKE  :keyword
OR CONVERT(  `owner` 
USING utf8 ) LIKE  :keyword
OR CONVERT(  `title` 
USING utf8 ) LIKE  :keyword
OR CONVERT(  `city` 
USING utf8 ) LIKE  :keyword
OR CONVERT(  `district` 
USING utf8 ) LIKE  :keyword
OR CONVERT(  `type` 
USING utf8 ) LIKE  :keyword
OR CONVERT(  `payrate` 
USING utf8 ) LIKE  :keyword
OR CONVERT(  `hours` 
USING utf8 ) LIKE  :keyword
OR CONVERT(  `description` 
USING utf8 ) LIKE  :keyword
)
LIMIT 0 , 30");

$sth->bindParam(':city', $city);
$sth->bindParam(':district', $district);
$sth->bindParam(':type', $type);
$sth->bindParam(':keyword', "%$keyword%");
$sth->execute();