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

时间:2016-04-15 21:52:28

标签: php mysql

有人可以告诉我这段代码有什么问题吗?我一直试图从我的数据库中获取数小时的值,结果总是为空。除了用户名和密码。

<?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);
?>

问题似乎出现在mysqli_stmt_bind_result行,但我无法弄明白。变量的顺序是正确的。我与数据库有连接,一切正常,但结果为空。

这是JSON的结果:

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

1 个答案:

答案 0 :(得分:0)

好的,所以,我已经设法用下面的代码解决了我的问题。我不知道以前的代码有什么问题。如果有人碰巧知道该问题的解决方案,请在评论中回答,我很好奇。注意:该代码仅用于测试建议。

<?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);
?>