2010-11-04 87 views
21

我使用選擇這樣的,它成功地獲取記錄:如何在Zend中更新數據庫表記錄?

$table = new Bugs(); 
$select = $table->select(); 
$select->where('bug_status = ?', 'NEW'); 
$rows = $table->fetchAll($select); 

,但現在我想更新相同的記錄。例如在簡單的MySQL中。

UPDATE TableName Set id='2' WHERE id='1'; 

如何在Zend中執行上面的查詢?

感謝

回答

39
$data = array(
    'field1' => 'value1', 
    'field2' => 'value2' 
); 
$where = $table->getAdapter()->quoteInto('id = ?', $id) 

$table = new Table(); 

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

它正在工作。謝謝 – Awan 2010-11-04 14:17:50

+4

你先生爲'$ where = $ table-> getAdapter() - > quoteInto('id =?',$ id)'快捷方式獲得獎勵標記。 – 2012-01-25 14:46:04

+1

是的,先生。 – pltvs 2012-01-25 15:26:59

1
$data = array(
    "field1" => "value1", 
    "field2" => "value2" 
); 

$where = "id = " . $id; 

$table = new Table(); 

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

您有錯誤的where子句。看到我的答案。 – pltvs 2010-11-04 13:56:16

11

既然你已經獲取你想要更改的行,似乎簡單到只是做:

$row->id = 2; 
$row->save(); 
9

以防萬一你想增加一列使用Zend_Db_Expr 例如:

$table->update(array('views' => new Zend_Db_Expr('views + 1')),$where); 
2
public function updateCampaign($id, $name, $value){ 
    $data = array(
     'name' => $name, 
     'value' => $value, 
    ); 
    $this->update($data, 'id = ?', $id); 
} 
1

對於多個where語句使用以下內容。

$data = array(
    "field1" => "value1", 
    "field2" => "value2" 
); 
$where['id = ?'] = $id; 
$where['status = ?'] = $status; 

$table = new Table(); 

$table->update($data, $where); 
相關問題