• <bdo id='eE7Ne'></bdo><ul id='eE7Ne'></ul>
    1. <i id='eE7Ne'><tr id='eE7Ne'><dt id='eE7Ne'><q id='eE7Ne'><span id='eE7Ne'><b id='eE7Ne'><form id='eE7Ne'><ins id='eE7Ne'></ins><ul id='eE7Ne'></ul><sub id='eE7Ne'></sub></form><legend id='eE7Ne'></legend><bdo id='eE7Ne'><pre id='eE7Ne'><center id='eE7Ne'></center></pre></bdo></b><th id='eE7Ne'></th></span></q></dt></tr></i><div id='eE7Ne'><tfoot id='eE7Ne'></tfoot><dl id='eE7Ne'><fieldset id='eE7Ne'></fieldset></dl></div>
      <legend id='eE7Ne'><style id='eE7Ne'><dir id='eE7Ne'><q id='eE7Ne'></q></dir></style></legend>
    2. <tfoot id='eE7Ne'></tfoot>

      <small id='eE7Ne'></small><noframes id='eE7Ne'>

      PDO lastInsertId() 总是返回 0

      时间:2023-09-20
        <tbody id='1OmPv'></tbody>

          <i id='1OmPv'><tr id='1OmPv'><dt id='1OmPv'><q id='1OmPv'><span id='1OmPv'><b id='1OmPv'><form id='1OmPv'><ins id='1OmPv'></ins><ul id='1OmPv'></ul><sub id='1OmPv'></sub></form><legend id='1OmPv'></legend><bdo id='1OmPv'><pre id='1OmPv'><center id='1OmPv'></center></pre></bdo></b><th id='1OmPv'></th></span></q></dt></tr></i><div id='1OmPv'><tfoot id='1OmPv'></tfoot><dl id='1OmPv'><fieldset id='1OmPv'></fieldset></dl></div>

          <small id='1OmPv'></small><noframes id='1OmPv'>

        • <tfoot id='1OmPv'></tfoot>

            1. <legend id='1OmPv'><style id='1OmPv'><dir id='1OmPv'><q id='1OmPv'></q></dir></style></legend>

              • <bdo id='1OmPv'></bdo><ul id='1OmPv'></ul>
              • 本文介绍了PDO lastInsertId() 总是返回 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我遇到了一个问题.我的框架在 PHP 5.3.0 上工作得很好.我将 PHP 版本升级到 PHP 5.4.x,并且我的框架的某些部分开始出现一些问题.

                I've come across with a problem. My framework was working just fine with PHP 5.3.0. I upgraded my PHP version to PHP 5.4.x and I started to have few issues with some parts of my framework.

                PHP版本升级后,PDO lastInsterId()总是返回0.

                After PHP version upgrade, PDO lastInsterId() always returns 0.

                我有一个名为 id 的自增字段.它正在将数据添加到数据库中,没有任何问题.

                I have auto-increment field called id. It is adding the data to database without any problems.

                出于某种原因,我一直将 0 作为最后一个插入 id.

                For some reason I keep getting 0 as last insert id.

                这是我的代码;

                databaseobjects.php

                public static function create () {
                        global $db;
                        $attributes = self::sanitize(static::$fields);
                
                        $sql  = "INSERT INTO ".PREFIX.static::$table_name." (";
                        $sql .= join(", ", array_keys($attributes));
                        $sql .= ") VALUE (:";
                        $sql .= join(", :", array_keys($attributes));
                        $sql .= ")";
                
                        return ($db->crudQuery($sql, $attributes)) ? true : false;
                    }
                
                public static function lastInsertID () {
                        global $db;
                        return $db->handler->lastInsertId();
                    }
                

                database.php

                public function crudQuery($sql, $data) {
                        $sth = $this->handler->prepare($sql);
                        return $sth->execute($data);
                    }
                

                首先调用create()方法,然后调用crudQuery()方法.正如我之前提到的,我可以成功地将数据添加到 MySQL 数据库中.不幸的是,当我调用 lastInsterID() 方法时,它总是返回 0.

                First create() method is called, then crudQuery() method is called. As I mentioned before, I can add the data successfully to MySQL database. Unfortunately when I call lastInsterID() method, it always returns 0.

                如果您能在我使用 SQL 查询获得最后一个 ID 之前帮我解决这个问题,我将非常高兴 (:

                I will be really glad if you can help me out with this problem before I will get the last ID with SQL Query (:

                推荐答案

                除了 php/PDO 或您的框架中的错误之外,还有两种可能性.lastInsertId() 在与插入不同的 MySQL 连接上调用,或者您在应用程序/框架中生成 id 并插入它,而不是让 auto_increment 为您生成它.表中哪一列是主键/auto_increment?该列是否包含在 create() 函数的 $attributes 中?

                Other than a bug in php/PDO or your framework, there are two possibilities. Either lastInsertId() is called on a different MySQL connection than the insert, or you are generating the id in your application/framework and inserting it, rather than letting auto_increment generate it for you. Which column in the table is the primary key/auto_increment? Is that column included in $attributes in your create() function?

                您可以测试 PDO 以确保该部分使用此代码(在新文件中)正常工作:

                You can test PDO to make sure that part is working correctly with this code (in a new file):

                // Replace the database connection information, username and password with your own.
                $conn = new PDO('mysql:dbname=test;host=127.0.0.1', 'user', 'password');
                
                $conn->exec('CREATE TABLE testIncrement ' .
                            '(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50))');
                $sth = $conn->prepare('INSERT INTO testIncrement (name) VALUES (:name)');
                $sth->execute([':name' => 'foo']);
                var_dump($conn->lastInsertId());
                $conn->exec('DROP TABLE testIncrement');
                

                当我运行这个脚本时,输出是

                When I ran this script, the output was

                string(1) "1"
                

                这篇关于PDO lastInsertId() 总是返回 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:PDO 最后插入 ID 总是正确的吗? 下一篇:PDO 无缓冲查询

                相关文章

                最新文章

              • <tfoot id='FtElN'></tfoot>
                <i id='FtElN'><tr id='FtElN'><dt id='FtElN'><q id='FtElN'><span id='FtElN'><b id='FtElN'><form id='FtElN'><ins id='FtElN'></ins><ul id='FtElN'></ul><sub id='FtElN'></sub></form><legend id='FtElN'></legend><bdo id='FtElN'><pre id='FtElN'><center id='FtElN'></center></pre></bdo></b><th id='FtElN'></th></span></q></dt></tr></i><div id='FtElN'><tfoot id='FtElN'></tfoot><dl id='FtElN'><fieldset id='FtElN'></fieldset></dl></div>

                <legend id='FtElN'><style id='FtElN'><dir id='FtElN'><q id='FtElN'></q></dir></style></legend>
              • <small id='FtElN'></small><noframes id='FtElN'>

                    • <bdo id='FtElN'></bdo><ul id='FtElN'></ul>