普通网友 2013-09-23 17:04
浏览 56
已采纳

如果stmt->执行时没有进入else语句,则为false

Can someone tell me what I am doing wrong here ...

This php runs a select query and then returns the result as a json encode. It would seem the if($stmt->execute())is not working correctly, if I put in an incorrect username and password I am getting [] instead of the "Incorrect email or password!", its not going into the else statement.

$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();

EDIT !

I tried checking for rows , but this gives me a new problem, the below results in Fatal error: Call to a member function 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)) {
        // ...
    }
}
  • 写回答

3条回答 默认 最新

  • drbe16008 2013-09-24 07:16
    关注

    I managed to get it to work by running a separate query if the first returned rows ...

    $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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?