PHP方法不返回MySQL身份

时间:2015-10-02 18:14:49

标签: php android json

我希望在注册时获取USER获得的ID(AUTO_INCREAMENT)并在android中解析它以在下一个Activity中使用它,当我注册成功的Value给出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;
}

1 个答案:

答案 0 :(得分:0)

我把它写成未经测试的代码(我已经很久没有使用MySQLi程序的东西......如果可以,请考虑使用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;
}

我忘记了格式参数......