检查数据库查询始终返回NULL

时间:2016-04-03 23:31:43

标签: php mysql

我正在使用mysql来托管数据库,我正在检查'收藏'表以查看是否存在条目。当用户点击收藏夹按钮时,将填充该表。当我在邮递员上运行查询时,我回显查询结果,即使数据库中的条目存在,我也返回NULL。

$conn = mysqli_connect($servername, $username, $password,$db);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}   

// json response array
$response = array("error" => FALSE);

if (isset($_POST['memory_id']) && isset($_POST['uid'])) {

    // receiving the post params
            $memory_id=$_POST['memory_id'];
        $user_id=$_POST['uid'];
        echo $memory_id;
        echo $user_id;
            $result = mysqli_query($conn, "SELECT * FROM favorite WHERE memory_id='$memory_id' AND user_id='$user_id'");
        if($result)
        {
         echo json_encode($result);
          // user failed to store
                $response["favorite"] = TRUE;
                echo json_encode($response);


        }
            else {
                    $response["favorite"]=FALSE;
            echo json_encode($response);
            }
        }


  else {
    $response["error"] = TRUE;
    $response["error_msg"] = "Required parameters user or memory is missing!";
    echo json_encode($response);
}

mysqli_close($conn);
?>

1 个答案:

答案 0 :(得分:0)

也许你忘了调用一些mysqli fetch函数(比如mysqli_fetch_assoc):

$q = mysqli_query($conn, "SELECT * FROM favorite WHERE memory_id='$memory_id' AND user_id='$user_id'");
$result = mysqli_fetch_assoc($q);
if($result)
{
     echo json_encode($result);
......

注意:假设您最喜欢的:: memory_id和favorite :: user_id列是VARCHAR类型