duandong9195 2016-04-15 21:52
浏览 64

PHP mysqli stmt绑定结果没有获取值

Can someone tell me what is the problem with this code? I've been trying to get the values from my database for hours and the results are always null.. Except for the username and password.

<?php
$con = mysqli_connect("aaaaa", "bbbbb", "cccccc", "ddddd");

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

$statement = mysqli_prepare($con, "SELECT * FROM table WHERE email = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $email, $password);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $id, $full_name, $email, $password, $distance, $average_rating, $home_address, $work_address);

$response = array();
$response["success"] = false;

while(mysqli_stmt_fetch($statement)){
    $response["success"] = true;
    $response["full_name"] = $full_name;
    $response["email"] = $email;
    $response["password"] = $password;
    $response["distance"] = $distance;
    $response["average_rating"] = $average_rating;
    $response["home_address"] = $home_address;
    $response["work_address"] = $work_address;
}

echo json_encode($response);
?>

The problem seems to be in the mysqli_stmt_bind_result line, but I can't figure it out. The order of the variables is correct. I have a connection with the database and everything is OK, but the results are null.

Here's the JSON result:

{"success":true,"full_name":null,"email":"aaaaaaaaa","password":"bbbbbbb","distance":null,"average_rating":null,"home_address":null,"work_address":null}
  • 写回答

1条回答 默认 最新

  • doujianjian2060 2016-04-15 23:19
    关注

    Okay, so, I've managed to solve my problem with the code below. I have no idea what was the problem with the previous code. If someone happen to know the solution to that problem, please answer in the comments, I am curious. Note: The code is only for test proposes.

    <?php
    $con = mysqli_connect("aaa", "bbb", "ccc", "ddd");
    
    $email = $_POST["email"];
    $password = $_POST["password"];
    
    $sql = "SELECT * FROM table";
    $result = mysqli_query($con, $sql);
    
    
    $response = array();
    $response["success"] = false;
    
    while($row = mysqli_fetch_assoc($result)){
        if($row["email"]==$email && $row["password"] == $password){
            $response["success"] = true;
            $response["full_name"] = $row["full_name"];
            $response["email"] = $row["email"];
            $response["password"] = $row["password"];
            $response["distance"] = $row["distance"];
            $response["average_rating"] = $row["average_rating"];
            $response["home_address"] = $row["home_address"];
            $response["work_address"] = $row["work_address"];
            break;
        }
    }
    
    echo json_encode($response);
    ?>
    
    评论

报告相同问题?