PHP, MySql, mysqli insert_id Returns 0 Always

System / development characteristics;

  • Windows 7 Ultimate x64 SP1 + all updates
  • PHP Version 5.5.15 Non-Thread Safe (x64 Experimental)
  • MySQL Server 5.6 (x64)
  • Php mysqli

I am executing a stored procedure that inserts username and password into a table with an AUTO_INCREMENT id field 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 Connection and Statement affected_rows, and num_rows (Statement) fields.
END

      

I've wrapped mysqli in my class, in short:

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

      

Testing this in the index.php page;

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]);

      

Give way, Affected Lines: 1 .

The insert was successful. I also need the insert id from this command, but the insert_id for the mysqli connection and the statement is 0. From var_dump;

var_dump to connect:

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 for operator:

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)}

      

Interestingly, there is an "id" field that returns the required id from a field id

in my table. Can anyone understand why insert_id is returning 0.

Thank you and welcome, NSKK

+3


source to share


1 answer


Try writing a stored procedure with a variable instead of calling row_count()

twice:

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 Connection and Statement affected_rows, and num_rows (Statement) fields.
END;

      



I think the second call is for the operator if

.

+1


source







All Articles