2

如果我想使用该Zend_Db_Table->update()方法用数据更新我的表,我无论如何都找不到在“where”子句中使用绑定变量。

方法签名是:

int  update($data, array|string $where)

通常你会这样调用方法:

$table = new Bugs();

$data = array(
    'updated_on'      => '2007-03-23',
    'bug_status'      => 'FIXED'
);

$where = $table->getAdapter()->quoteInto('bug_id = ?', 1234);

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

quoteInto只是要转义变量,而不是绑定它。

需要有一种方法来使用绑定变量,否则 DBMS 不会有效地缓存这个查询。

我是否遗漏了什么,或者这是 Zend 的疏忽?

4

1 回答 1

4

您只是在更新数据,RDBMS(我假设 MySQL)不会缓存 UPDATE 查询。如果您仍想使用绑定变量(安全性?性能?),您将不得不使用准备好的语句:

$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$stmt = $db->prepare("UPDATE table SET key = :key, value = :value");

foreach ($data as $key=>$value) {
    $stmt->bindParam('key', $key);
    $stmt->bindParam('value', $value);
    $stmt->execute();
}

但是,除非您批量处理数百万个 UPDATE 查询,否则我认为您不应该为此烦恼。只需使用$table->update($data, $where);

于 2009-10-22T22:01:41.780 回答