繁体   English   中英

从doctrine和本机mysql查询中获取最后一个插入的ID

[英]Get last inserted ID from doctrine and native mysql query

我在symfony2中使用了doctrine。 但我使用本机mysql查询来插入一些数据。 现在我想要从数据库中最后插入的id。 我的代码看起来像:

$stmt = $this->getDoctrine()->getManager()
             ->getConnection()
             ->prepare("INSERT INTO tb_company (v1,v2,v3) values('$v1','$v2','$v3')");
$stmt->execute();

所以要恢复上次插入的id我试过了

$stmt->insert_id; 

$id=LAST_INSERT_ID(id)

$id=$stmt->getId();

还有很多其他的。 他们都没有为我工作。 我是学说的新手。 如何从这里获取最后插入的ID? 我错过了什么?

我想你需要这样的东西:

$conn = $this->getDoctrine()->getConnection();

$stmt = $conn->prepare('INSERT INTO tb_company (v1, v2, v3) values(:v1, :v2, :v3)');
$stmt->bindValue('v1', $v1);
$stmt->bindValue('v2', $v2);
$stmt->bindValue('v3', $v3);
$stmt->execute();

$id = $conn->lastInsertId();

你不能链接bindValueexecute因为它们返回一个boolean

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM