php - 使用 $_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 如果我要直接初始化变量,我会得到正确的输出
解决方案
看起来 $response 在您使用 array_push 之前没有被初始化,所以当您尝试回显时 $response 仍然为空。
推荐阅读
- typescript - 如何删除角度5中的选定或所有复选框
- xamarin.forms - 我可以通过非 http(s) URL 启动我的跨平台应用程序吗?
- java - JFileChooser Buttons CustomisationsGradient Paint for OptionPane
- unix - awk 更改 shell 变量
- ssis - 无法在 SSIS 2017 中的 excel 连接管理器上使用表达式
- php - mysqli插入查询返回false
- html - Panda to CSV 所有数据都没有被写入
- sql - 使用 VBA 和 SQL,我可以在查询中引用 excel 列标题吗?
- javascript - 如何执行在 JSDOM 中动态创建的另一个脚本节点?
- c# - BeginSend 不立即返回