-1

有人可以告诉我我在这里做错了什么......

此 php 运行选择查询,然后将结果作为 json 编码返回。if($stmt->execute())如果我输入错误的用户名和密码,我会得到 [] 而不是“不正确的电子邮件或密码!”,这似乎无法正常工作,它不会进入 else 语句。

$email = $_POST['email'];
$password = $_POST['password'];

//check user exists
$qry = "SELECT u.uid, u.name, u.email, u.registration_date, u.updated_at, d.device_name, d.device_registration_date
FROM users u
LEFT JOIN devices d
ON d.parent_id = u.uid
WHERE u.email = ? AND u.password = ? ";
$stmt = $mysqli->prepare($qry);
$stmt->bind_param('ss', $email, $password);
if ($stmt->execute()) {
    $result = $stmt->get_result();

    while ($row = $result->fetch_array(MYSQLI_ASSOC)) {     
        $response["success"] = 1;
        $response["device"][]=$row; 
    }
    echo json_encode($response);
} else {
    // user not found
    $response["fail"] = 0;
    $response["error_msg"] = "Incorrect email or password!";
    echo json_encode($response);
    exit;
}
$stmt->close();

编辑 !

我尝试检查 rows ,但这给了我一个新问题,以下导致致命错误:调用成员函数 fetch_array()

$stmt = $mysqli->prepare($qry);
$stmt->bind_param('ss', $email, $password);
$stmt->execute();
$stmt->fetch();
$stmt->store_result();
if ($stmt->num_rows >= 0) {
    $result = $stmt->get_result();  
    while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
        // ...
    }
}
4

3 回答 3

2

它只是因为该语句仍然成功而返回,即使它没有返回任何行。例如,当您的 SQL 中有错误时,查询不成功。

相反,您需要计算返回的行数。

于 2013-09-23T17:06:22.103 回答
0

尝试这个:

$email = $_POST['email'];
$password = $_POST['password'];

//check user exists
$qry = "SELECT u.uid, u.name, u.email, u.registration_date, u.updated_at, d.device_name, d.device_registration_date
FROM users u
LEFT JOIN devices d
ON d.parent_id = u.uid
WHERE u.email = ? AND u.password = ? ";
$stmt = $mysqli->prepare($qry);
$stmt->bind_param('ss', $email, $password);
$stmt->execute();
$stmt->store_results();

$resultRows = $stmt->num_rows;
$stmt->free_result();

if($resultRows >= 1){
    $result = $stmt->get_result();

    $row = $result->fetch_array(MYSQLI_ASSOC);

    $response["success"] = 1;
    $response["device"][]=$row;

    echo json_encode($response);
}
else {
    // user not found
    $response["fail"] = 0;
    $response["error_msg"] = "Incorrect email or password!";
    echo json_encode($response);
    exit;
}
$stmt->close();
于 2013-09-23T17:21:40.997 回答
-1

如果第一个返回的行...,我设法通过运行单独的查询使其工作...

$email = $_POST['email'];
    $password = $_POST['password'];

    //check user exists
    $qry = "SELECT * FROM users WHERE email = ? AND password = ? ";
    $stmt = $mysqli->prepare($qry);
    $stmt->bind_param('ss', $email, $password);
    $stmt->execute();
    $stmt->store_result();
    $resultRows = $stmt->num_rows;
    $stmt->free_result();
    if($resultRows >= 1){

        $qry = "SELECT u.uid, u.name, u.email, u.registration_date, u.updated_at, d.device_name, d.device_registration_date
        FROM users u
        LEFT JOIN devices d
        ON d.parent_id = u.uid
        WHERE u.email = ? AND u.password = ? ";
        $stmt = $mysqli->prepare($qry);
        $stmt->bind_param('ss', $email, $password);
        $stmt->execute();
        $result = $stmt->get_result();

        while($row = $result->fetch_array(MYSQLI_ASSOC))
        {       
                $response["success"] = 1;
                $response["device"][]=$row;

        }
        echo json_encode($response);


    }
    else {
        // user not found
        $response["fail"] = 0;
        $response["error_msg"] = "Incorrect email or password!";
        echo json_encode($response);
        exit;
    }
    $stmt->close();
于 2013-09-24T07:16:30.443 回答