删除数据库中的相关信息(Doctrine)

  1. 作者QQ:67065435 QQ群:821635552
<?php
$orm = $this->getDoctrine()->getEntityManager();
$product = $orm->getRepository('DBBundle:User')->find($user_id);
if (!empty($product)){
    $orm->remove($product);
    $orm->flush();
}

删除数据库中相关信息(PDO)

<?php
# 最简单的方式执行(exec返回受影响行数)
$this->dbh = $this->get('database_connection');
$sql = <<<SQL
DELETE
FROM
  `user`
WHERE
  `user`.id = 1
SQL;
$res = $this->dbh->exec($sql);
unset($this->dbh);

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

# 按key绑定参数执行(execute返回执行bool结果)
$this->dbh = $this->get('database_connection');
$sql = <<<SQL
DELETE
FROM
  `user`
WHERE
  `user`.id = :id
SQL;
$id = 10000;
$sth = $this->dbh->prepare($sql);
$sth->bindParam('id', $id);
$res = $sth->execute();
unset($sth);
unset($this->dbh);
Copyright © 豆包嘿嘿~ 2012-∞ 冀ICP备17033181号 all right reserved,powered by Gitbook修订: 2012-12-31 12:00:00

results matching ""

    No results matching ""