且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 where 子句中使用 Zend_Db_Table->update() 绑定变量

更新时间:2023-10-23 15:54:40

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

You are only updating data, RDBMS (I assume MySQL) doesn't cache UPDATE queries. If you still want to use bind variables (security? performance?), you will have to use prepared statements:

$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);

But unless you are having millions of UPDATE queries in a batch I don't think you should bother with this. Just use the $table->update($data, $where);