数据库
首页 > 数据库> > PHP,MySql,mysqli insert_id返回0 Always

PHP,MySql,mysqli insert_id返回0 Always

作者:互联网

系统/开发规范;

> Windows 7 Ultimate x64 SP1所有更新
> PHP版本5.5.15非线程安全(x64实验)
> MySQL Server 5.6(x64)
> Php mysqli

我正在执行一个存储过程,该过程将用户名和密码插入到具有AUTO_INCREMENT id INT(11)PK字段的表中.

PROCEDURE `user_account_create`(IN userName VARCHAR(32), IN userPasskey VARCHAR(254))
BEGIN
    START TRANSACTION;
    INSERT INTO user_account (`name`, passkey) VALUES (userName, userPasskey);
    IF (ROW_COUNT() = 1) THEN COMMIT; ELSE ROLLBACK; END IF;
    SELECT ROW_COUNT() AS affected_rows; -- Used for PHP mysqli's Connection and Statement affected_rows, and num_rows (Statement) fields.
END

简而言之,我已经在我自己的课堂上包装了mysqli;

namespace DataAccess\Broker {

    final class MySqliDb {
        private $conn;

        public function __construct($dbHost, $dbUser, $dbPass, $dataBase) {
            $this->conn = new \mysqli($dbHost, $dbUser, $dbPass, $dataBase);}

        public function ExecuteStatement($cmdText, array $paramValue = null) {
            $affected = -1;
            $stmt = $this->CreateStatement($cmdText, paramValue);
            $stmt->execute();
            // echo 'insert_id' . $this->conn->insert_id;
            $stmt->store_result();
            $affected = $stmt->affected_rows;
            stmt->close();
            return $affected;
        }

        // ... other functions that utilse CreateStatement below

        private function CreateStatement($cmdText, array $paramValue = null) {
            $stmt = $this->conn->prepare($cmdText);

            if ($paramValue !== null) {
                $params = [];
                foreach ($paramValue as $p => &$v) {$params[$p] = &$v;}
                call_user_func_array([$stmt, 'bind_param'], $params);
            }
            return $stmt;
        }

    }  // class
} // namespace

在index.php页面上测试它;

use \DataAccess\Broker\MySqliDb as mysqldb;
$db = new mysqldb('127.0.0.1', 'root', '', 'thedb');
$types = 'ss'; $user_name = 'its_me'; $pass_key = 'a-hashed-password';
echo 'Affected Rows: ' . $db->ExecuteStatement('CALL user_account_create(?,?)', [$types, $user_name, $pass_key]);

将产量,受影响的行:1.

插入成功.我也需要来自此命令的insert id,但mysqli连接和语句的insert_id都是0.来自var_dump;

var_dump用于连接:

object(mysqli)#2 (19) {
["affected_rows"] => int(1)
["client_info"] => string(79) "mysqlnd 5.0.11-dev - 20120503 - $Id: xxx$" ["client_version"] => int(50011)
["connect_errno"] => int(0) ["connect_error"] => NULL
["errno"] => int(0) ["error"] => string(0) "" ["error_list"] => array(0) { }
["field_count"] => int(1)
["host_info"] => string(20) "127.0.0.1 via TCP/IP" ["info"] => NULL
["insert_id"] => int(0)
["server_info"] => string(6) "5.6.20" ["server_version"] => int(50620)
["stat"] => NULL
["sqlstate"] => string(5) "HY000"
["protocol_version"] => int(10)
["thread_id"] => int(6)
["warning_count"] => int(0)}

声明的var_dump:

object(mysqli_stmt)#3 (10) {
["affected_rows"] => int(1)
["insert_id"] => int(0)
["num_rows"] => int(1)
["param_count"] => int(6)
["field_count"] => int(1)
["errno"] => int(0) ["error"] => string(0) "" ["error_list"] => array(0) { }
["sqlstate"] => string(5) "00000"
["id"] => int(1)}

有趣的是,有一个字段“id”,即从我表中的字段id中撤回所需的id.任何人都可以看到为什么insert_id返回0.

感谢致敬,
NJC

解决方法:

尝试使用变量编写存储过程,而不是两次调用row_count():

PROCEDURE `user_account_create`(IN userName VARCHAR(32), IN userPasskey VARCHAR(254))
BEGIN
    START TRANSACTION;
    INSERT INTO user_account (`name`, passkey) VALUES (userName, userPasskey);
    IF ((@rc := ROW_COUNT) = 1) THEN COMMIT; ELSE ROLLBACK; END IF;
    SELECT @rc AS affected_rows; -- Used for PHP mysqli's Connection and Statement affected_rows, and num_rows (Statement) fields.
END;

我认为第二个调用是指if语句.

标签:php,mysql,mysqli,insert-id
来源: https://codeday.me/bug/20190708/1403939.html