PHP方法不返回MySQL标识


PHP method not returning MySQL identity

我想在注册时获得用户获得的ID (auto_increment)并在android中解析它以在下一个活动中使用它,当我注册成功的值时给出1,它工作,但ID的三个值返回值'{}'。

public function createNewRegisterUser($username, $password, $email){        
$query = "insert into users (username, password, email) values ('$username', '$password', '$email')"; 
    $inserted = mysqli_query($this->db->getDb(), $query);
    if($inserted == 1){ 
        $json['success'] = 1;
        $json['id'] = mysqli_query($this->db->getdb(),"select id from users where username = '$username'" );    ;                       
    }else{
        $json['success'] = 0;
        $json['id'] = ""    ;
    }
    mysqli_close($this->db->getDb());
    return $json;
}

我把这个写为未经测试的代码(我很久没有使用过mysql过程的东西了…如果可以的话,考虑使用PDO):

public function createNewRegisterUser($username, $password, $email){
   $dbh = $this->db->getDb(); // assuming this returns a handle?
   $sth = mysqli_prepare($dbh, 'insert into users (username, password, email) values (?, ?, ?)');
   mysqli_stmt_bind_param($sth, 'sss', $username, $password, $email);
   mysqli_stmt_execute($sth);
   $errNo = mysqli_stmt_errno($sth);
   $userId = mysqli_insert_id ($dbh);
   if($errNo !== 0) { 
        $json['success'] = 1;
        $json['id'] = $userId;
    }else{
        $json['success'] = 0;
        $json['id'] = ""    ;
    }
    mysqli_close($dbh);
    return $json;
}

我忘了格式参数…