【问题标题】:SQLSTATE[HY093]: All parameters are filled properly and the syntax is right?SQLSTATE[HY093]:所有参数都填写正确,语法是否正确?
【发布时间】:2016-02-16 02:53:40
【问题描述】:

我收到以下错误:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in

代码如下:

$transactions_sql = "INSERT INTO transactions (usr, service, txn_id, orig_amount, currency, date, description, fee_amt, 
  fee_currency, fee_descr, fee_type, net_amt, status) VALUES ";
$transactions_sql_data = array_fill(0, count($transactions), "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$transactions_sql .= implode(",", $transactions_sql_data);
$stmt = $conn->prepare($transactions_sql);
$i = 1;
foreach ($transactions["data"] as $tr) {
  $stmt->bindValue($i++, $id, PDO::PARAM_INT);
  $stmt->bindValue($i++, $tr["service"], PDO::PARAM_STR);
  $stmt->bindValue($i++, $tr["id"], PDO::PARAM_INT);
  $stmt->bindValue($i++, $tr["amount"], PDO::PARAM_INT);
  $stmt->bindValue($i++, $tr["currency"], PDO::PARAM_STR);
  $stmt->bindValue($i++, $tr["created"], PDO::PARAM_STR);
  $stmt->bindValue($i++, $tr["description"], PDO::PARAM_STR);
  $stmt->bindValue($i++, $tr["fee_details"][0]["amount"], PDO::PARAM_INT);
  $stmt->bindValue($i++, $tr["fee_details"][0]["currency"], PDO::PARAM_STR);
  $stmt->bindValue($i++, $tr["fee_details"][0]["description"], PDO::PARAM_STR);
  $stmt->bindValue($i++, $tr["fee_details"][0]["type"], PDO::PARAM_STR);
  $stmt->bindValue($i++, $tr["net"], PDO::PARAM_INT);
  $stmt->bindValue($i++, $tr["status"], PDO::PARAM_STR);
}
$stmt->execute();

执行var_dump($transactions_sql) 打印"INSERT INTO balance_transactions (usr, service, txn_id, orig_amount, currency, date, description, fee_amt, fee_currency, fee_descr, fee_type, net_amt, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?),(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?),(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",这是正确的(就问号的数量而言-$tr 数组内只有三个数组)。

【问题讨论】:

  • bindValue 的第一个参数在循环迭代中保持不变,因此看起来下一组值替换了前一组值,而不是作为新参数添加
  • 那会很奇怪,因为我用this answer作为参考。
  • 当他使用常量参数名时,他每次循环迭代都执行一次语句,所以参数不会发生冲突。在单个查询示例中,他使用 $i++ 而不是常量整数,因此不会重复使用相同的索引
  • 你能检查一下吗? $stmt->bindValue(2, tr["service"], PDO::PARAM_STR); 。不是$tr["service"] 吗?
  • count($transactions) - 1 - 1?

标签: php mysql


【解决方案1】:

事实证明,我必须将 -1 添加到 count($transactions) 才能正常工作。看起来它添加了太多的参数集。

以下是有效的: $transactions_sql_data = array_fill(0, count($transactions), "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

感谢其他人的帮助!

【讨论】:

    猜你喜欢
    • 2017-06-21
    • 2021-06-07
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 2011-06-07
    相关资源
    最近更新 更多