doupu2722 2019-04-06 17:22
浏览 88
已采纳

响应PHP / JSON的空数组

I'm coding a php server sending arrays response using JSON. Now i simply testing the php code but the result is empty:

{"routes":[]}

There is my code in PHP:

<?php

require_once 'connect.php';

if ($_SERVER['REQUEST_METHOD'] =='POST') {

    $region = $_POST['region'];

    $sql = "SELECT * FROM routes WHERE region='$region'";

    $result = mysqli_query($conn, $sql);

    $dbdata = array();

    while ($row = $result->fetch_assoc()) {
        $dbdata[] = $row;
    }

    $obj = (object)[

        'routes' => $dbdata
    ];

    echo json_encode($obj, JSON_UNESCAPED_SLASHES);
}
?>

I'am testing in postman, sending POST:

http://MY PATH/filter.php?"region"=West
  • 写回答

2条回答 默认 最新

  • duanfeigui6655 2019-04-06 17:30
    关注

    1) Add

    error_reporting(E_ALL); 
    

    to the top of the script. If there is anything funny it will show the errors.

    2) Change the post to a get for testing purposes 3) Go to the page

     http://MY PATH/filter.php?"region"=West //-> remove the quotes
     http://MY PATH/filter.php?region=West //->This is what the request will look like
    

    4) Do we see anything and what? 5) If there is nothing use variables to test things

     ex: 
         $region = $_GET['region'];
    
    echo $region; //Should print West
    
    $sql = "SELECT * FROM routes WHERE region='$region'";
    
    $result = mysqli_query($conn, $sql);
    echo "<pre>";
    print_r($results) // and so on until you see something you can use
    $dbdata = array();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名