PHP,MySql,mysqli insert_id返回0 Always

时间:2014-08-14 02:37:59

标签: php mysql mysqli insert-id

系统/开发规范;

  • Windows 7旗舰版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

1 个答案:

答案 0 :(得分:1)

尝试使用变量编写存储过程,而不是两次调用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语句。