使用$ _POST时服务器响应为null

时间:2019-04-24 02:02:31

标签: php android mysql

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}

但是如果我取消注释示例输出并注释另一部分,则输出为: {“结果”:123}

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

1 个答案:

答案 0 :(得分:1)

在使用array_push之前,似乎$ response尚未初始化,因此当您尝试回显时,$ response仍然为空。