如果我想使用 Zend_Db_Table->update()
方法用数据更新我的表,我无论如何都找不到在“where”子句中使用绑定(bind)变量。
方法签名是:
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
只是转义变量,而不是绑定(bind)它。
需要有一种方法来使用绑定(bind)变量,否则 DBMS 将无法有效地缓存此查询。
我是否遗漏了什么,或者这是 Zend 的疏忽?
最佳答案
您只是更新数据,RDBMS(我假设是 MySQL)不会缓存 UPDATE 查询。如果您仍然想使用绑定(bind)变量(安全性?性能?),则必须使用准备好的语句:
$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);
关于zend-framework - 如何在 where 子句中使用 Zend_Db_Table->update() 绑定(bind)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1604388/