JSON输出为空

时间:2018-07-16 00:23:38

标签: php

我有以下PHP代码返回一些JSON编码的结果。如果我进行双重编码(即在将当前行添加到数组的位置放入json_encode),则会得到过度处理(很多“ /”符号到处都是)但输出完整。就在这里,我什么也没得到。

$link = mysqli_connect("localhost","steph_stephen", "haltermann1!", "stephenh_tanks");
if (!$link){
    die("Conection failed: " . mysqli_connect_error());
}
$query="SELECT * FROM Tank";
$result = mysqli_query($link, $query);
if (mysqli_num_rows($result) > 0){
    $myArray = array();
    header('Content-type:application/json');
    while($tank = mysqli_fetch_assoc($result))
    {
        //$myArray[] = json_encode($tank); if i leave this line in (and comment the line below) - i get the full but over-process json result
        $myArray[] = $tank; //without the line above no json string is returned
    }
    print json_encode($myArray);
}
else
{
    echo "Could not execute query";
    echo mysqli_error($link);
}
mysqli_close($link);

以下是返回的JSON的一小部分(尽管已处理完毕)

["{\"tank\":\"001\",\"product\":\"Caromax 20HN B\\\/F\",\"prod_code\":\"51533\",\"tank_level\":\"7888\",\"volume\":\"1325818\",\"pump_volume\":\"1275282\",\"max_volume\":\"1759741\",\"tank_status\":\"For Test\"}"...

0 个答案:

没有答案