无法合并2个JSON结果并从PHP脚本获取响应

时间:2017-11-16 07:06:06

标签: php json

我正在尝试从我的select语句中获取结果,我正在使用2个语句并希望获得2个输出的JDON响应。就是这样。

问题:我正在测试Chrome Postman,它正在显示结果

[
null,
null
]

以下是我的PHP代码;我哪里错了?

<?php
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
    header('Access-Control-Allow-Headers: Content-Type,x-prototype-version,x-requested-with');
    header('Cache-Control: max-age=900');
    header("Content-Type: application/json"); // tell client that we are sending json data

    define("DB_USR","erSKJV");
    define("DB_PsAS","T");
    define("DB","ipaddress/orcl.profits");
    $conn = oci_connect(DB_USR,DB_PAS,DB);

    if (!$conn) 
    {
        $e = oci_error();
        $result["message"] = $e['message'];
        echo json_encode($result);
        exit(1);
    } 

        $loggedinuser = $_POST['loggedinuser'];

        $stmt = "select count(*) as taskgiven from admtask where CLOSE_DT is null and primary='".$loggedinuser."' ";

        $stmt1 = "select count(*) as mytask from admtask where CLOSE_DT is null and entusr='".$loggedinuser."' ";

        $result=oci_parse($conn,$stmt);
        $ex2=oci_execute($result);

        while ($row=oci_fetch_assoc($result))
        {
          $stmta[] = $row;
        }
         json_encode($stmta);

        $result1=oci_parse($conn,$stmt1);
        $ex2=oci_execute($result1);

        while ($row1=oci_fetch_assoc($result1))
        {
          $stmtb[] = $row1;
        }
         json_encode($stmtb);

        $final[] = json_decode($stmta,true);
        $final[] = json_decode($stmtb,true);
        $json_merge = json_encode($final);

        echo json_encode($json_merge);


?>

你需要专注于stmta,stmtb,最后是json_merge

2 个答案:

答案 0 :(得分:1)

您不应该在包含JSON的数组上调用json_encode()。将原始数组放入容器数组中,然后在整个事件上调用json_encode()

$final = array($stmta, $stmtb);
echo json_encode($final);

答案 1 :(得分:0)

json_encode返回json的字符串。您需要在前两个编码期间将其分配给变量。

while ($row=oci_fetch_assoc($result))
    {
      $stmta[] = $row;
    }
    $stmta =  json_encode($stmta);

    $result1=oci_parse($conn,$stmt1);
    $ex2=oci_execute($result1);

    while ($row1=oci_fetch_assoc($result1))
    {
      $stmtb[] = $row1;
    }
     $stmtb = json_encode($stmtb);