我正在编写一个Android应用程序,当用户登录时,我还想发送一个MySQL数据库列作为JSON响应的一部分。如何将MySQL列信息转换为数组,如何将其包含在JSON响应中?这是我的尝试:
以下是我的PHP代码:
if ($tag == 'login') {
// Request type is check Login
$email = $_POST['email'];
$password = $_POST['password'];
// check for user
$user = $db->getUserByEmailAndPassword($email, $password);
$groups = $db->getUserGroups();
if ($user != false) {
// user found
$response["groups"] = $groups;
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["user_group"] = $user["user_group"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$result = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
// check for result
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
$result = mysql_fetch_array($result);
$salt = $result['salt'];
$encrypted_password = $result['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $result;
}
} else {
// user not found
return false;
}
}
/**
*Return the user_group column of mysql database
*/
public function getUserGroups() {
$groups = mysql_query("SELECT user_group FROM users") or die(mysql_error());
return $groups;
}