php Zend 如何更新数据库表记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4097234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:56:33  来源:igfitidea点击:

How update a database table record in Zend?

phpmysqlsqlzend-frameworkzend-db

提问by Awan

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.

但现在我想更新相同的记录。例如在简单的 MySQL 中。

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

How to execute above query in Zend ?

如何在 Zend 中执行上述查询?

Thanks

谢谢

回答by Alex Pliutau

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

$table = new Table();

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

回答by Tim Lytle

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

由于您已经在获取要更改的行,因此执行以下操作似乎最简单:

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

回答by Srinivas R

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

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

$table->update(array('views' => new Zend_Db_Expr('views + 1')),$where);

回答by Waqas Bukhary

For more than one where statement use the following.

对于多个 where 语句,请使用以下语句。

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

$table = new Table();

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

回答by zendframeworks

public function updateCampaign($id, $name, $value){
    $data = array(
        'name' => $name,
        'value' => $value,
    );
    $this->update($data, 'id = ?', $id );
}

回答by mitch

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

$where = "id = " . $id;

$table = new Table();

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