当前位置: 首页 > 面试题库 >

如何从PDO获取最后插入的插入行ID

边银龙
2023-03-14
问题内容

我正在使用PHP中的mvc结构,我想检索最后插入的行ID。

我创建了以下sql代码:

$sql = "INSERT INTO song (artist, track, link) VALUES (:artist, :track, :link)";
$query = $this->db->prepare($sql);
$query->execute(array(':artist' => $artist, ':track' => $track, ':link' => $link));

echo $query->lastInsertId(); // To retrieve last inserted row ID.

但不幸的是我遇到了这个错误: Fatal error: Call to undefined method PDOStatement::lastInsertId()

我也尝试过此堆栈链接,但不适用于我,因此如果您能帮助我获取ID,我将非常高兴。

我也在这里共享我的controller.php文件。

/**
 * This is the "base controller class". All other "real" controllers extend this class.
 */
class Controller{
    /**
     * @var null Database Connection
     */
    public $db = null;

    /**
     * Whenever a controller is created, open a database connection too. The idea behind is to have ONE connection
     * that can be used by multiple models (there are frameworks that open one connection per model).
     */
    function __construct(){
        $this->openDatabaseConnection();
    }

    /**
     * Open the database connection with the credentials from application/config/config.php
     */
    private function openDatabaseConnection(){
        // set the (optional) options of the PDO connection. in this case, we set the fetch mode to
        // "objects", which means all results will be objects, like this: $result->user_name !
        // For example, fetch mode FETCH_ASSOC would return results like this: $result["user_name] !
        // @see http://www.php.net/manual/en/pdostatement.fetch.php
        $options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING);

        // generate a database connection, using the PDO connector
        // @see http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/
        $this->db = new PDO(DB_TYPE . ':host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS, $options);
    }

    /**
     * Load the model with the given name.
     * loadModel("SongModel") would include models/songmodel.php and create the object in the controller, like this:
     * $songs_model = $this->loadModel('SongsModel');
     * Note that the model class name is written in "CamelCase", the model's filename is the same in lowercase letters
     * @param string $model_name The name of the model
     * @return object model
     */
    public function loadModel($model_name){
        require 'application/models/' . strtolower($model_name) . '.php';
        // return new model (and pass the database connection to the model)
        return new $model_name($this->db);
    }
}

问题答案:

你快到了。

如果您查看lastInsertId的手册页,则会在数据库句柄上调用它-
您当前正在语句中对其进行调用。

您只需要致电:

$this->db->lastInsertId();


 类似资料:
  • 问题内容: 我有一个查询,我想插入最后一个ID。字段ID是主键,并且是自动递增的。 我知道我必须使用以下语句: 该语句适用于如下查询: 但是,如果我想使用以下语句获取ID: 我收到此错误: 我究竟做错了什么? 问题答案: 那是因为那是SQL函数,而不是PHP。您可以使用。 喜欢: 如果要使用SQL而不是PDO API进行操作,则可以像普通的select查询一样进行操作:

  • 问题内容: 我正在将PHP PDO与PostgreSQL一起用于新项目。 给定以下功能,如何返回刚刚插入的行的ID?它不像现在那样工作。 问题答案: 从手册: 根据基础驱动程序,返回最后插入的行的ID或序列对象的最后一个值。例如,PDO_PGSQL()要求您为name参数指定序列对象的名称。 应该是这样的: [编辑]更新文档链接

  • 我插入批量插入laravel 5像这样: 知道如何获取最后一个插入的id吗?

  • 问题内容: 我正在掌握PDO的基础知识。 但是我试图获取插入的行的ID,我使用: 我遇到的教程都是关于交易的,但是我没有使用交易! 问题答案: 您可能正在寻找lastInsertId。“返回最后插入的行或序列值的ID”。

  • 问题内容: 我想获取表中最后一个ID插入的值。我该怎么做? 问题答案: 好吧,我使用的解决方案是: 这将从插入数据库的最后一行获取id列:)

  • 问题内容: 使用PDO运行以下查询(实际上,我使用准备好的语句,但存在相同的问题) 如何获得有关和的记录的ID ? 从字面上返回最后一个ID。 采取这个最后的ID,减去记录数并假定范围覆盖了我的所有记录,是否足够?会有差距/跳跃。是否保证此查询是原子查询? 问题答案: 如果您使用的是MyISAM表,则由于表级别的锁定机制,您只能获得一定范围的ID。 阅读http://dev.mysql.com/d