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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?