Kohana 3 中的 insert_id

发布于 2024-10-03 11:44:08 字数 507 浏览 0 评论 0原文

我正在使用 Kohana 3 框架和 Mysql 存储过程。如何获取最后插入的记录的 id? 代码如下:

class Model_MyModel extends Kohana_Model
{
    public function insertNew($param1, $param2)
    {
        $result = $this->_db->query(Database::INSERT, 'CALL insertNew('.$param1.', '.$param2.', false)';
        return $result;
    }
    ...
    ...
}

文档说,执行插入查询时,query() 方法返回一个数组,其中包含最后一个插入 id 和受影响的行数。当我打电话时: print_r($result) 我得到: 大批 ( [0] => 0 [1] => 1 ) insert_id 键为 0,尽管我在数据库中有很多记录。 我做错了什么?

I'm using Kohana 3 framework with Mysql stored procedures. How can I get id of the last inserted record?
Here's the code:

class Model_MyModel extends Kohana_Model
{
    public function insertNew($param1, $param2)
    {
        $result = $this->_db->query(Database::INSERT, 'CALL insertNew('.$param1.', '.$param2.', false)';
        return $result;
    }
    ...
    ...
}

Documentation says, the query() method returns an array with the last insert id and affected rows number, when executing an insert query. When I call: print_r($result)
I'm getting:
Array
(
[0] => 0
[1] => 1
)
The insert_id key is 0, though I'm having many records in the db.
What I'm doing wrong?

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

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

发布评论

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

评论(1

墟烟 2024-10-10 11:44:08

我认为在使用过程插入后,您必须使用 SQL 的 LAST_INSERT_ID():(

SELECT LAST_INSERT_ID() as last_insert_id FROM table_name

在您的过程中最后定义此查询)。

本例中的问题是 Kohana 自动返回 mysql_insert_id 和 mysql_affected_rows 作为 Database::INSERT 的结果,因此您需要将该过程作为 SELECT 查询调用并获取它 (Database::SELECT)。

I think you'll have to use SQL's LAST_INSERT_ID() after inserting using a procedure:

SELECT LAST_INSERT_ID() as last_insert_id FROM table_name

( in your procedure just define this query in the end ).

The problem in this case is that Kohana automatically returns mysql_insert_id and mysql_affected_rows as result for Database::INSERT, so you'll need to call the procedure as a SELECT query and fetch it (Database::SELECT).

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