PDO - 没有警告的不匹配的绑定变量数

时间:2015-08-18 22:59:32

标签: php mysql pdo

是否有办法允许通过PDO::Execute传递的参数数量大于prepare语句中标识的令牌数量?

例如:

$john = Array("name"=>"John", "lastname"=>"Smith","occupation"=>"Citizen");

$pdo = $db->prepare("SELECT * FROM Users WHERE name = :name AND lname = :lastname");

$result = $pdo->execute($john);

在此示例中,PDO会发出警告:

Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]:
Invalid parameter number:
number of bound variables does not match number of tokens in...

是否可以将PDO设置为忽略没有匹配令牌名称的其他参数?

1 个答案:

答案 0 :(得分:0)

我假设您无法关闭此错误,但这对于解决此问题

如果您发现错误,请检查其特定错误,如果忽略它,那会有效吗?

这样的事情:

try {
    $john = Array("name"=>"John",
                  "lastname"=>"Smith",
                  "occupation"=>"Citizen"
                 );

    $pdo = $db->prepare("SELECT * 
                         FROM Users 
                         WHERE name = :name 
                           AND lname = :lastname"
                       );

    $result = $pdo->execute($john);
}
catch (PDOException $e) {
    if ( $e->getCode() !== 'HY093' ) {
        // report error like normal
        exit;
    }
}