22

I am using select like this and it is fetching record successfully:

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

But Now I want to update same record. For example in simple MySQL.

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

How to execute above query in Zend ?

Thanks

Charles
  • 50,943
  • 13
  • 104
  • 142
Awan
  • 18,096
  • 36
  • 89
  • 131

6 Answers6

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

$table = new Table();

$table->update($data, $where);
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
12

Since you're already fetching the row you want to change, it seems simplest to just do:

$row->id = 2;
$row->save();
Tim Lytle
  • 17,549
  • 10
  • 60
  • 91
10

just in case you wanna increment a column use Zend_Db_Expr eg:

$table->update(array('views' => new Zend_Db_Expr('views + 1')),$where);
j0k
  • 22,600
  • 28
  • 79
  • 90
Srinivas R
  • 101
  • 1
  • 2
4

For more than one where statement use the following.

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

$table = new Table();

$table->update($data, $where);
Syed Waqas Bukhary
  • 5,130
  • 5
  • 47
  • 59
2
public function updateCampaign($id, $name, $value){
    $data = array(
        'name' => $name,
        'value' => $value,
    );
    $this->update($data, 'id = ?', $id );
}
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
1
   $data = array(
    "field1" => "value1",
    "field2" => "value2"
);

$where = "id = " . $id;

$table = new Table();

$table->update($data, $where);
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
mitch
  • 282
  • 2
  • 7