修改数据库中的信息(Doctrine)

$orm = $this->getDoctrine()->getManager('default');
$user = $orm->getRepository('DBBundle:User')->find($id);
if (!empty($user)) {
    $user->setName('新数据');
    $orm->flush();
}

更新数据库中相关信息(PDO)

# 最简单的方式执行(exec返回受影响行数)
$this->conn = $this->get('database_connection');
$sql = <<<SQL
UPDATE
  `user`
SET
  `user`.login_time = `user`.login_time + 1
WHERE
  `user`.id = 1
SQL;
$res = $this->conn->exec($sql);
unset($this->conn);

# 按序绑定参数执行(execute返回执行bool结果)
$this->conn = $this->get('database_connection');
$sql = <<<SQL
UPDATE
  `user`
SET
  `user`.login_time = `user`.login_time + 1
WHERE
  `user`.id IN(?,?,?)
SQL;
$do_exec = $this->conn->prepare($sql);
$do_exec->bindValue(1, 10000);
$do_exec->bindValue(2, 10086);
$do_exec->bindValue(3, 95001);
$res = $do_exec->execute();
unset($do_exec);
unset($this->conn);

# 按key绑定参数执行(execute返回执行bool结果)
$this->conn = $this->get('database_connection');
$sql = <<<SQL
UPDATE
  `user`
SET
  `user`.login_time = `user`.login_time + 1
WHERE
  `user`.id = :id
SQL;
$id = 10000;
$do_exec = $this->conn->prepare($sql);
$do_exec->bindParam('id', $id);
$res = $do_exec->execute();
unset($do_exec);
unset($this->conn);
@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2019-07-08 16:49:59

results matching ""

    No results matching ""