$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
));
据我所知,变量的数量确实与标记的数量相匹配。 我只是想不出这里的错误。
最佳答案
你只需要传递一个数组给execute方法。因此,您更新后的代码将如下所示:
$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/en/pdostatement.execute.php
PDOStatement::execute — Executes a prepared statement
Usage: public bool PDOStatement::execute ([ array $input_parameters ] )
Here is the example from the documentation:
<?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));
关于php - PDOStatement::execute(): SQLSTATE[HY093]: 参数编号无效:绑定(bind)变量的数量与标记的数量不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34762475/