绑定变量的数量与令牌的数量不匹配

绑定变量的数量与令牌的数量不匹配

本文介绍了PDOStatement :: execute():SQLSTATE [HY093]:无效的参数编号:绑定变量的数量与令牌的数量不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$sql = 'INSERT INTO `' . $table_name . '` '
            . '(`day`, `campaign_name`, `campaign_id`, `views`, `CPM`, `cost`, `currency`, `cost_EUR`) VALUES '
            . '(:day, :campaign_name, :campaign_id, :views, :CPM, :cost, :currency, :cost_EUR)';

$this->_dbi->execute($sql, array(
            ':day'        => $day,
            ':campaign_name'      => $campaignName,
            ':campaign_id'    => $campaignID,
            ':views'          => $views,
            ':CPM'        => $cpm,
            ':cost'     => $cost_EUR,
            ':currency'       => 'EUR',
            ':cost_EUR' => $cost_EUR
));

据我所知,变量的数量确实与标记的数量匹配.我只是无法在这里找出错误.

As far as I can tell the number of variables do match the number of tokens.I just can't figure out the error here.

推荐答案

您只需要将数组传递给execute方法.因此,您更新后的代码将如下所示:

You only need to pass an array to the execute method. So your updated code would look like this:

$sql = 'INSERT INTO `' . $table_name . '` '
            . '(`day`, `campaign_name`, `campaign_id`, `views`, `CPM`, `cost`, `currency`, `cost_EUR`) VALUES '
            . '(:day, :campaign_name, :campaign_id, :views, :CPM, :cost, :currency, :cost_EUR)';
$sth = $this->_dbi->prepare($sql);
$sth->execute(array(
            ':day'        => $day,
            ':campaign_name'      => $campaignName,
            ':campaign_id'    => $campaignID,
            ':views'          => $views,
            ':CPM'        => $cpm,
            ':cost'     => $cost_EUR,
            ':currency'       => 'EUR',
            ':cost_EUR' => $cost_EUR
));

在此处了解更多信息: http://php.net/manual/zh-cn/pdostatement. execute.php

Read more here: http://php.net/manual/en/pdostatement.execute.php

用法:公共布尔PDOStatement :: execute([array $ input_parameters])

以下是文档中的示例:

<?php
/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour');
$sth->execute(array(':calories' => $calories, ':colour' => $colour));

这篇关于PDOStatement :: execute():SQLSTATE [HY093]:无效的参数编号:绑定变量的数量与令牌的数量不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 17:26
查看更多