首页 > 解决方案 > 使用 $_POST 时服务器响应为空

问题描述

php代码:

<?php
header("Content-type:application/json");
require "db_conn.php";
$user_name = $_POST["user_name"];
$user_pass = $_POST["pwd"];
$mysql_qry = "select * from user_accounts where user_name like '$user_name' and pwd like '$user_pass';";
$result = mysqli_query($conn,$mysql_qry);

if( $num_rows = mysqli_num_rows($result) > 0 ){
while( $row = mysqli_fetch_array($result)){
array_push($response,array("user_name"=>$row['user_name'],"first_name"=>$row['first_name'] , "last_name"=>$row['last_na$}
echo json_encode(array("server_response"=>$response));

//echo json_encode(array("result"=>123));
}else{
echo "login fail";
}

?>

我的回复是:{"server_response":null}

但是如果我取消注释示例输出并注释另一部分,我会得到输出:{“result”:123}

也出于某种原因而不是 $_POST 如果我要直接初始化变量,我会得到正确的输出

标签: phpandroidmysql

解决方案


看起来 $response 在您使用 array_push 之前没有被初始化,所以当您尝试回显时 $response 仍然为空。


推荐阅读