PDO绑定错误

时间:2016-04-08 10:02:33

标签: php pdo

我尝试从db

获取一些值
$client_ids = array('client_id' => $this->arParams['client_id']);
 print_r($client_ids);
 $client_ids_in = implode(',', array_fill(0, count($client_ids), '?'));
 $query = "SELECT odc.curr_id FROM office.dictionary_currency AS odc LEFT JOIN office.adwords_clients_google AS oacg ON odc.curr_code = oacg.client_currency WHERE oacg.client_id IN ($client_ids_in)";
        $google_currency = $this->DB->prepare($query);
        $google_currency->execute($client_ids);
        $google_currency->setFetchMode(PDO::FETCH_ASSOC);
        $google_currency = $google_currency->fetch();
        $google_currency = $google_currency['curr_id'];

$client_ids看起来像

Array
(
    [client_id] => 15087
)

$query

SELECT odc.curr_id FROM office.dictionary_currency AS odc LEFT JOIN office.adwords_clients_google AS oacg ON odc.curr_code = oacg.client_currency WHERE oacg.client_id IN (?)

我收到错误

PHP Warning:  PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in /var/www/instruments/reports/report.php on line 531

表示行中的错误

$google_currency = $this->DB->prepare($query);

出了什么问题?如何解决?

2 个答案:

答案 0 :(得分:1)

更改您的代码

 $client_ids = array('client_id' => $this->arParams['client_id']);

类似

 $client_ids = array($this->arParams['client_id']);

所以$client_ids就像

Array
(
   0 => 15087
)

答案 1 :(得分:1)

如果使用通用占位符?,则需要传递以执行非关联数组。 因此,您可以从阵列中删除'client_id'键,或使用命名占位符。

可能的解决方案

$client_ids = array($this->arParams['client_id']);