doupu2722 2019-04-06 09: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 09: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部