简体   繁体   English

如何获取MVC中最后一个插入的ID?

[英]How to get the ID of the last insert in MVC?

I have a $mydata->save(); 我有一个$mydata->save(); in my statement to insert a new row, and the next row in the action I need the ID of the inserted row. 在我的语句中插入新行,然后在操作的下一行中,我需要插入行的ID How can I get it? 我怎么才能得到它?

$id=$this->db->insert_id();

获取最后插入的ID。

If $myData is a model then : 如果$myData是模型,则:

$result = $myData->save();
$id = $result->id;

Kind of same like in Laravel. 有点像Laravel。

Although if you are using some MVC CMS like Joomla then it should be like: 虽然如果您使用的是Joomla之类的MVC CMS,则应该像这样:

$db = JFactory::getDBO();
$db->setQuery( $query );
$db->query();

$id = $db->insertid();

And also if you have liberty to run raw queries then simply use: 而且,如果您有运行原始查询的自由,那么只需使用:

mysql_insert_id();

So it really depends upon you particular situation. 因此,这实际上取决于您的特定情况。

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

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