删除数据库中的相关信息(Doctrine)
作者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);