zend框架使用execute获取多行插入的最后一个插入id

发布于 2024-08-18 13:05:06 字数 410 浏览 2 评论 0原文

如何使用多行插入获取最后插入的 ID? 这是我的代码:

$sql='INSERT INTO t (col1, col2, col3) VALUES (1, 2, 3), (4, 5, 6), (7, 8, 9)'; // example
$stmt = $contactsTable->getAdapter()->prepare($sql);
$stmt->execute(); 
$rowsAdded=$stmt->rowCount(); // mysql_affected_rows
$lastId=$stmt->lastInsertId();
echo '<br>Last ID: '.$lastId;

另外,ZF 中是否有一种方法可以在插入之前获取下一个插入 id?

谢谢

How would I get the last inserted ID using a multirow insert?
Here is my code:

$sql='INSERT INTO t (col1, col2, col3) VALUES (1, 2, 3), (4, 5, 6), (7, 8, 9)'; // example
$stmt = $contactsTable->getAdapter()->prepare($sql);
$stmt->execute(); 
$rowsAdded=$stmt->rowCount(); // mysql_affected_rows
$lastId=$stmt->lastInsertId();
echo '<br>Last ID: '.$lastId;

Also, is there a method in ZF to get the next insert id before an insert?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

﹎☆浅夏丿初晴 2024-08-25 13:05:06
$lastId=$contactsTable->getAdapter()->lastInsertId();

这奏效了。

$lastId=$contactsTable->getAdapter()->lastInsertId();

This worked.

殤城〤 2024-08-25 13:05:06

因此,这是我用来创建多行插入的完整工作代码,获取添加的行和最后插入的 id:

$contactsTable = new Contacts_Model_Contacts();
$sql='INSERT INTO t (col1, col2, col3) VALUES (1, 2, 3), (4, 5, 6), (7, 8, 9)'; // example
$stmt = $contactsTable->getAdapter()->prepare($sql);
$stmt->execute(); 
$rowsAdded=$stmt->rowCount(); // mysql_affected_rows
$lastId=$contactsTable->getAdapter()->lastInsertId(); // last inserted id
echo '<br>Last ID: '.$lastId;

So, here is the full working code I'm using to create a multi-row insert, getting the rows added and the last inserted id:

$contactsTable = new Contacts_Model_Contacts();
$sql='INSERT INTO t (col1, col2, col3) VALUES (1, 2, 3), (4, 5, 6), (7, 8, 9)'; // example
$stmt = $contactsTable->getAdapter()->prepare($sql);
$stmt->execute(); 
$rowsAdded=$stmt->rowCount(); // mysql_affected_rows
$lastId=$contactsTable->getAdapter()->lastInsertId(); // last inserted id
echo '<br>Last ID: '.$lastId;
谷夏 2024-08-25 13:05:06

替代解决方案。从控制器中移出 SQL 代码并将它们放入模型中。这就是他们的目的。

如果您使用模型,您可以在类变量中给出具有自动增量列的表的名称。

protected $_name="<table_name>"; 

然后在你的模型类方法中,你可以从中获取最后的插入ID

$insertID= $this->getAdapter()->lastInsertId();

An alternate solution. Move off sql code from controllers and place them in models. That is what they are for.

If you are using models, you can given the name of table which has auto increment column, in class variable.

protected $_name="<table_name>"; 

Then in your model class method, you can get last insert id from

$insertID= $this->getAdapter()->lastInsertId();
若能看破又如何 2024-08-25 13:05:06

该代码应该可以工作,但它只会为您提供最后一次插入的 id。

您可以使用此 mysql 查询获取下一个自动增量:

SELECT Auto_increment FROM information_schema.tables WHERE TABLE_SCHEMA = 'your_db_name' AND TABLE_NAME='the_table_you_want';

that code should work, but it will only get you the id of your last insert.

you can get the next autoincrement with this mysql query:

SELECT Auto_increment FROM information_schema.tables WHERE TABLE_SCHEMA = 'your_db_name' AND TABLE_NAME='the_table_you_want';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文