令牌声明时PDO中的参数无效

时间:2014-08-06 19:28:26

标签: php mysql pdo

我有正确执行的查询:

$query = $handler->prepare("SELECT * FROM pm WHERE ((user1 = $userid AND user1read = 'no') OR (user2 = $userid AND user2read = 'no') AND id2 = '1')");
$query->execute();

当我尝试声明令牌时,会抛出Invalid parameter number'致命错误。这是抛出错误的代码:

$query = $handler->prepare("SELECT * FROM pm WHERE ((user1 = :userid AND user1read = 'no') OR (user2 = :userid AND user2read = 'no') AND id2 = '1')");
$query->execute(array(
'userid' => $userid
));

我尝试从sql迁移到PDO。这里有一些sql残留,我不在吗?

2 个答案:

答案 0 :(得分:3)

You cannot use a named parameter marker of the same name twice in a prepared statement

来源:http://php.net/manual/de/pdo.prepare.php

尝试:

$query = $handler->prepare("SELECT * FROM pm WHERE ((user1 = :userid AND user1read = 'no')     OR (user2 = :userid2 AND user2read = 'no') AND id2 = '1')");
$query->execute(array(
  'userid' => $userid,
  'userid2' => $userid
));

答案 1 :(得分:1)

+1到@djdy。但是,如果我没有弄错的话,你可以尝试使用PDO::ATTR_EMULATE_PREPARES,它会起作用:

$handler->setAttribute(PDO::ATTR_EMULATE_PREPARES, TRUE);
$query = $handler->prepare("SELECT * FROM pm WHERE ((user1 = :userid AND user1read = 'no')     OR (user2 = :userid AND user2read = 'no') AND id2 = '1')");
$query->execute(array(
'userid' => $userid,
));