PDO Prepared语句未捕获异常错误消息

时间:2014-01-13 19:07:00

标签: php pdo

我正在尝试运行此PDO预备声明

$stmt = $pdo_conn->prepare(
    "INSERT into email_attachments (email_seq, attachment) 
     values (:email_seq, :attachment) ");
$stmt->execute(array(
    ':email_seq' => $admin_email_sequence, 
    ':attachment' => $_SERVER["DOCUMENT_ROOT"].'/'.$settings["ticket_files_folder"].'/'.$ticketnumber.'-'.$currentDate.'-'.$at[filename], $at[attachment]
));

但是我收到了这个错误:

  

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in /home/integra/public_html/autocheck/support_emails.php:662 Stack trace: #0 /home/integra/public_html/autocheck/support_emails.php(662): PDOStatement->execute(Array) #1 {main} thrown in /home/integra/public_html/autocheck/support_emails.php on line 662

2 个答案:

答案 0 :(得分:2)

:attachment条目的最后,你有一个逗号,而不是我认为应该是一个句号。

.$at[filename], $at[attachment]
              ^-- here

这会导致异常,因为查询中有2个标签,数组中有3个元素。

答案 1 :(得分:1)

你的执行数组中有一个额外的,你还有一个额外的数组元素( $ at [attachment]

试试此代码

$stmt = $pdo_conn->prepare("INSERT into email_attachments (email_seq, attachment) values (:email_seq, :attachment) ");
$stmt->execute(array(
    ':email_seq' => $admin_email_sequence, 
    ':attachment' => $_SERVER["DOCUMENT_ROOT"] . '/' . $settings["ticket_files_folder"] . '/' . $ticketnumber . '-' . $currentDate . '-' . $at[filename]
));