【问题标题】:PDO Exception "SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens"PDO 异常“SQLSTATE[HY093]:无效的参数号:绑定变量的数量与标记的数量不匹配”
【发布时间】:2015-01-06 08:11:24
【问题描述】:

我不熟悉 PDO 扩展,也找不到我的代码有什么问题。 代码sn-p:

                try {
                $this->PDO->exec('SET AUTOCOMMIT = 0');
                $this->PDO->exec('START TRANSACTION');

                $this->PDO->prepare("UPDATE office_users
                                     SET balance = balance - ?
                                     WHERE id = ?")
                          ->execute(array($sbs_price, $this->user->id)
                );
                $user_balance -= $sbs_price;

                $this->PDO->prepare("UPDATE office_company
                                     SET pay_days = pay_days + ?
                                     WHERE inn = ?
                                     AND user_id = ?")
                          ->execute(array(
                                $sbs_period_days,
                                $company_inn,
                                $this->user->id)
                );

                $fin_string = $company_name.' ИНН '.$company_inn.' продление '.$sbs_period_month. ' мес.';

                $this->PDO->prepare("INSERT INTO office_fin_transactions
                                     (user_id, date_register, dsc, amount, status)
                                     VALUES (?, ?, ?, ?, ?)")
                          ->execute(array(
                                  $this->user->id.
                                  date("Y-m-d H:i:s"),
                                  $fin_string,
                                  $sbs_price,
                                  0)
                          );


                $this->PDO->exec("COMMIT");

                } catch (PDOException $Exception) {
                    $this->PDO->exec("ROLLBACK");
                    echo json_encode(array('result' => false,
                                            'error' => $Exception->getMessage()));
                    exit;
                }

            echo json_encode(array('result'=>'success',
                                   'inn' => $company_inn,
                                   'sbs_period' => $sbs_period_month,
                                   'company_name' => $company_name,
                                   'balance' => $user_balance)
            );
            exit;

在执行此代码之前,在脚本开始时,PDO 是这样配置的:

        $this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

之前的所有查询都不会生成 PDO 异常,但最后一个 INSERT 查询会。

变量的值:

$this->user->id == 158;
$fin_string == "ООО "Тестовые системы" - 2954 ИНН 123456 продление 6 мес."
$sbs_price == 1000;

office_fin_transactions 表的结构是:

怎么了?如果您有任何想法,请提供帮助。

【问题讨论】:

    标签: php mysql pdo


    【解决方案1】:

    $this->user->id 后面有一个点而不是逗号,所以它实际上只是一个 4 元素数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-15
      • 2017-04-27
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多