douji9734 2013-11-10 15:02
浏览 137

PDO - 带有问号参数的批量UPDATE语句,提示?

I would like to update a MySQL table containing up-to-date currency symbol & value pairs from a given exchange rates service once every 24 hours.

Right now, I do something like this:

foreach ($currencies->rates as $currency => $rate) {
    $connection->addQuery("update/{$currency}", "UPDATE `i18n/currencies` SET `rate` = ? WHERE `code` = ?;", $rate, $currency);
}

$connection->output();

I get the $currencies object out from a JSON-encoded response, $connection is a reference to my database connection class.

If I want to create a SQL query group (a class I created to perform bulk commands), the syntax would be like this:

if (!empty($this->attributes["queries"])) {
    $queries = implode(" ", array_map(function ($input) { return (mb_strpos($input->attributes["query"], ";") !== false ? $input->attributes["query"] : "{$input->attributes["query"]};"); }, $this->attributes["queries"]));
    $parameters = [];

    foreach ($this->attributes["queries"] as $query) {
        if (!empty($query->attributes["parameters"])) {
            $parameters = array_merge($parameters, $query->attributes["parameters"]);
        }
    }
}

This way, if I have the following set of queries (with their final values after the query):

UPDATE `i18n/currencies` SET `rate` = ? WHERE `code` = ?; [1.7, AED]
UPDATE `i18n/currencies` SET `rate` = ? WHERE `code` = ?; [2.1, BYR]
UPDATE `i18n/currencies` SET `rate` = ? WHERE `code` = ?; [0.7, EUR]
UPDATE `i18n/currencies` SET `rate` = ? WHERE `code` = ?; [1.1, GBP]

The final string would be like this:

UPDATE `i18n/currencies` SET `rate` = ? WHERE `code` = ?; UPDATE `i18n/currencies` SET `rate` = ? WHERE `code` = ?; UPDATE `i18n/currencies` SET `rate` = ? WHERE `code` = ?; UPDATE `i18n/currencies` SET `rate` = ? WHERE `code` = ?; [1.7, AED, 2.1, BYR, 0.7, EUR, 1.1, GBP]

The query is prepared and executed like this:

if ($output = $this->attributes["references"]["parent"]->attributes["link"]->prepare($queries, [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY])) {
    $result = ($output->execute((!empty($parameters) ? $parameters : null)) ? true : false);
}

But it's not working. I've tried different modifications to my code, but none of them seems to provide me with the solution I'm looking for.

Is it really possible to do BULK updates with PDO passing the parameters like this? Or is it another way I haven't discovered yet? Maybe it's not doable at all and I'm just being stubborn. I could rewrite this update script with single SQL queries, as my connection class will fire them up, from the first to the last one.

Can you give me a hint?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私