pdo不允许重用占位符 - 搜索多列的替代方法是什么?

时间:2014-02-24 09:46:28

标签: php pdo

我的代码是这样的:

Select * from tableX where tableX.column1 LIKE %:testString%' OR 
tableX.column2 Like '%:testString% OR ... '

现在显然这不起作用,因为testString是一个正在重复的命名占位符。我不能做testString2,testString3等,因为我会查找20-30列!这将是一种痛苦!

任何更简单的解决方法?

1 个答案:

答案 0 :(得分:-1)

您必须在参数中包含%符号,而不是在查询

  

$ testString =%testString%

还使用未命名的参数,? ,每个?需要一个单独的参数。使用命名参数可以避免这种情况。

$stmt = $dbh->prepare("Select * from tableX where tableX.column1 LIKE :testString
                      OR tableX.column2 Like :testString OR ... ");
$stmt->bindParam(':testString',$testString, PDO::PARAM_STR);
相关问题