Returning JSON from a PHP Script

前端 未结 18 1865
南方客
南方客 2020-11-21 04:59

I want to return JSON from a PHP script.

Do I just echo the result? Do I have to set the Content-Type header?

18条回答
  •  耶瑟儿~
    2020-11-21 05:27

    If you query a database and need the result set in JSON format it can be done like this:

    fetch_array()){
        $rows[] = $row;
    }
    //Return result to jTable
    $qryResult = array();
    $qryResult['logs'] = $rows;
    echo json_encode($qryResult);
    
    mysqli_close($db);
    
    ?>
    

    For help in parsing the result using jQuery take a look at this tutorial.

提交回复
热议问题