PDO绑定两个值数组


PDO binding two array of values

我在foreachINSERT状态中添加了很多带有params($queryArr)的值

    public function getClients()
{
    helper::putToLog("'n----- getClients'n", true);
    $managedCustomerService = $this->user->GetService('ManagedCustomerService', ADWORDS_VERSION);
    $selector = new Selector();
    $selector->fields = array("CustomerId", "Name", "CurrencyCode");
    $graph = $managedCustomerService->get($selector);
    if (isset($graph->entries)) {
        $accounts = [];
        foreach ($graph->entries as $account) {
            $accounts[$account->customerId] = $account;            
        }
        helper::putToLog('Clients found: '.count($accounts)."'n", true);
    } else {
        helper::putToLog('Clients not found'."'n", true);
        return false;
    }

    $sth = $this->db->prepare('UPDATE `adwords_clients_google` set status = 2');
    $sth->execute();
    $sth = null;

    $queryClients = "INSERT INTO `adwords_clients_google` (`client_foreign_id`, `status`, `client_name`, `client_currency`) VALUES";
    foreach($accounts as $account) {
        $queryArr[$account->customerId] = "(".$account->customerId.",  1, :".$account->customerId.", :".$account->customerId."_currencyCode)"; 
        $nameArr[$account->customerId] = $account->name;
        $currencyArr[$account->customerId."_currencyCode"] = $account->currencyCode;

    }
    $queryClients .= implode(',', $queryArr) . " ON DUPLICATE KEY UPDATE `status` = VALUES(`status`), `client_name` = VALUES(`client_name`) ";
    $sth = $this->db->prepare($queryClients);
    foreach ($nameArr as $key => $value) {
        $sth->bindValue(":$key", str_replace("'", "''", $value), PDO::PARAM_STR);
    }
    foreach ($currencyArr as $key => $value) {
        $sth->bindValue(":$key", $value, PDO::PARAM_STR);
    }
    print_r($sth);
    try {
        if ($sth->execute()) {
            helper::putToLog('ok queryCampaignArr, inserted rows: ' . $sth->rowCount());
        } else {
            helper::putToLog('not ok', true);
        }
    } catch (Exception $ex) {
        helper::putToLog($sth->debugDumpParams(), true);
        helper::putToLog("ERROR: ".$ex->getMessage(), true);
    }
    return true;
}

并且有2个值数组I需要绑定CCD_ 4和CCD_。我没有得到任何错误,但列client_currency是空的,即使数组$currencyArr包含所有必要的值。怎么了?

您似乎还没有掌握prepared+参数化语句的概念
您先准备它们一次,然后使用不同的参数(一次或多次)执行它们。

$sth = $this->db->prepare('
    INSERT INTO
        `adwords_clients_google`
        (`client_foreign_id`, `status`, `client_name`, `client_currency`)
    VALUES
        (:id, 1, :name, :currency)
    ON DUPLICATE KEY UPDATE
        `status` = VALUES(`status`),
        `client_name` = VALUES(`client_name`)
');
$sth->bindParam(':id', $id);
$sth->bindParam(':name', $name);
$sth->bindParam(':currency', $currency);
foreach($accounts as $account) {
    $id = $account->customerId;
    $name = $account->name;
    $currency = $account->currencyCode;
    $sth->execute();
}

如果您没有任何错误消息/日志条目,请确保您的PDO实例的错误处理模式确实设置为PDO::ERRMODE_EXCEPTION。

编辑:为了说明这一点,这里有一个sscce:

<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly', array(
    PDO::ATTR_EMULATE_PREPARES=>false,
    PDO::MYSQL_ATTR_DIRECT_QUERY=>false,
    PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
));
setup($pdo); // creating temporary table + sample data for this example
printTable("before", $pdo);
$sth = $pdo->prepare('
    INSERT INTO
        `soFoo`
        (`client_foreign_id`, `status`, `client_name`, `client_currency`)
    VALUES
        (:id, 1, :name, :currency)
    ON DUPLICATE KEY UPDATE
        `status` = VALUES(`status`),
        `client_name` = VALUES(`client_name`)
');
$sth->bindParam(':id', $id);
$sth->bindParam(':name', $name);
$sth->bindParam(':currency', $currency);
$accounts = data();
foreach($accounts as $account) {
    $id = $account->customerId;
    $name = $account->name;
    $currency = $account->currencyCode;
    $sth->execute();
}
printTable("after", $pdo);
function data() {
    return array_map(function($e) { return (object)$e; }, array(
        array('customerId'=>1, 'name'=>'customerA', 'currencyCode'=>'cA'),
        array('customerId'=>2, 'name'=>'customerB', 'currencyCode'=>'cB'),
    ));
}
function printTable($cap, $pdo) {
    echo $cap, "'r'n";
    foreach( $pdo->query('SELECT * FROM soFoo', PDO::FETCH_ASSOC) as $r ) {
        echo join(', ', $r), "'r'n";
    }
}
function setup($pdo) {
    $pdo->exec('
        CREATE TEMPORARY TABLE soFoo (
            client_foreign_id int,
            `status` int,
            client_name varchar(32),
            client_currency varchar(32),
            unique(client_foreign_id)
        )
    ');
    $pdo->exec("INSERT INTO soFoo (client_foreign_id,status,client_name,client_currency) VALUES (1, 0, 'agent smith', 'kruegerrand')");
}

打印

before
1, 0, agent smith, kruegerrand
after
1, 1, customerA, kruegerrand
2, 1, customerB, cB