我在服务器上安装了这段PHP代码,我也安装了DataBase。当我尝试通过URL http://ptyxiaki2016.eu.pn/login.php?email=tade&password=1234
运行它时,我的所有值都为NULL。
tade 1234 {“error”:false,“uid”:null,“user”:{“name”:null,“surname”:null,“country”:null,“email”:null,“password”日期null, “电话”:空}}
我的PHP代码如下:
<?php
require_once 'DB_Functions.php';
require_once 'DB_Connect.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (isset($_GET['email']) && isset($_GET['password'])) {
// receiving the post params
$email = $_GET['email'];
$password = $_GET['password'];
// get the user by email and password
$user = $db->getUserByEmailAndPassword($email, $password);
if ($user != false) {
// user is found
$response["uid"] = $user["oid"];
$response["error"] = FALSE;
$response["uid"] = $user["oid"];
$response["user"]["name"] = $user["name"];
$response["user"]["surname"] = $user["surname"];
$response["user"]["country"] = $user["country"];
$response["user"]["email"] = $user["email"];
$response["user"]["password"] = $user["password"];
$response["user"]["telephone"] = $user["telephone"];
echo json_encode($response);
} else {
// user is not found with the credentials
$response["error"] = TRUE;
$response["error_msg"] = "Login credentials are wrong. Please try again!";
echo json_encode($response);
}
} else {
// required post params is missing
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters email or password is missing!";
echo json_encode($response);
}
?>
这是我在登录时调用的函数getUserByEmailAndPassword()
。它位于我包含的DB_Functions.php中。
public function getUserByEmailAndPassword($email, $password) {
$stmt = $this->conn->prepare("SELECT * FROM owner WHERE email = ?");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
$user = $stmt->bind_result($oid, $name, $surname, $country, $email, $password, $telephone);
while ($stmt->fetch()) {
printf("%s %s\n", $email, $password);
}
$stmt->close();
return $user;
}
}
我在这里缺少什么?我的数据库没问题,但是,我该如何检索数据呢?
答案 0 :(得分:1)
错误在这里:
$user = $stmt->bind_result($oid, $name, $surname, $country, $email, $password, $telephone);
您将结果绑定到变量($ oid,$ name,...),但返回$ user。
bind_result
返回操作结果(true / false),但不返回查询结果。
因此调用getUserByEmailAndPassword()的结果是bool值。如果您将运行而不是echo json_encode($response)
echo json_encode($user)
,您将会看到它。
UPD:尝试使用此代码代替您的代码
if ($stmt->execute()) {
$stmt->bind_result($user["oid"], $user["name"], ...)
while ($stmt->fetch()) {
printf("%s %s\n", $email, $password);
}