PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

后端 未结 1 1127
天命终不由人
天命终不由人 2021-01-25 07:13
$sql = \'INSERT INTO `\' . $table_name . \'` \'
            . \'(`day`, `campaign_name`, `campaign_id`, `views`, `CPM`, `cost`, `currency`, `cost_EUR`) VALUES \'
                


        
1条回答
  •  不思量自难忘°
    2021-01-25 07:33

    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
    ));
    

    Read more here: 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:

    prepare('SELECT name, colour, calories
        FROM fruit
        WHERE calories < :calories AND colour = :colour');
    $sth->execute(array(':calories' => $calories, ':colour' => $colour));
    

    0 讨论(0)
提交回复
热议问题