gpt4 book ai didi

php - Zend Framework - 使用 WHERE 和 AND 更新数据库行

转载 作者:可可西里 更新时间:2023-11-01 13:48:37 24 4
gpt4 key购买 nike

要在 Zend Framework 的 MySQL 表行更新中使用 where,我有类似的东西:

public function updateBySiteId(array $data, $id) {
$table = $this->gettable();

$where = $table->getAdapter()->quoteInto('site_id = ?', $id);

return $table->update($data, $where);
}

我希望这能给我类似...

UPDATE foo SET ponies = 'sparkly' WHERE site_id = '1'

但是如果我想创建以下内容怎么办:

UPDATE foo SET ponies = 'sparkly' WHERE site_id = '1' AND type = 'zombie'

在手册中,我没有看到如何使用 quoteInto(或 quote 或其他一些安全方法...这可能只是意味着我找错了地方但是...叹息).

最佳答案

Since the table update() method proxies to the database adapter update() method, the second argument can be an array of SQL expressions. The expressions are combined as Boolean terms using an AND operator.

http://framework.zend.com/manual/en/zend.db.table.html

$data = array(
'updated_on' => '2007-03-23',
'bug_status' => 'FIXED'
);
$where[] = "reported_by = 'goofy'";
$where[] = "bug_status = 'OPEN'";
$n = $db->update('bugs', $data, $where);

结果 SQL 是:

UPDATE "bugs" SET "update_on" = '2007-03-23', "bug_status" = 'FIXED' WHERE ("reported_by" = 'goofy') AND ("bug_status" = 'OPEN')

http://framework.zend.com/manual/en/zend.db.adapter.html#zend.db.adapter.write.update

关于php - Zend Framework - 使用 WHERE 和 AND 更新数据库行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3756252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com