user2828442
user2828442

Reputation: 2515

Unable to merge 2 JSON results and get response from a PHP script

I am trying to fetch a result from my select statement, I am using 2 statements and want a JDON response of the 2 outputs. That's all.

Problem: I am testing from Chrome Postman, it is showing result

[
null,
null
]

Below is my php code; where am I going wrong?

<?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);


?>

You need to concentrate on stmta , stmtb and finally json_merge

Upvotes: 1

Views: 39

Answers (2)

Barmar
Barmar

Reputation: 781096

You shouldn't call json_encode() on an array that contains JSON. Put the original arrays in the container array, and then call json_encode() on the entire thing.

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

Upvotes: 1

Tangmole
Tangmole

Reputation: 1

json_encode returns the string for the json. You need to assign that to a variable during the first two encodings.

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);

Upvotes: 0

Related Questions