PDO绑定两个值数组

时间:2016-03-30 14:07:11

标签: php arrays pdo

我在$queryArr

中的INSERT statemnt中添加了params(foreach)的很多值
    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;
}

我需要绑定$nameArr$currencyArr两个值数组。我没有收到任何错误,但是列client_currency为空,即使数组$currencyArr包含所有必需的值。怎么了?

1 个答案:

答案 0 :(得分:1)

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

$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实例的error handling mode确实设置为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
相关问题