1

I am trying to generate JSON using PHP and SQLSRV. But it seems json_encode is not returning any results while var_dump shows the results. I am unable to figure out what's wrong with my code. I am using WAMP 2.0 under windows environment.

<?php
require_once '../inc/sql.php';
require_once '../inc/functions_sola.php';
?>

<?php 

    $ConnectionInfo=dbConnectDBServer();

    if ($ConnectionInfo == false) {
        die( print_r( sqlsrv_errors(), true));
    }

    $query1 = $SOLAEntityDetails;
    $temp = array();

    if (($result = sqlsrv_query($ConnectionInfo,$query1)) == null) {
        die(print_r(sqlsrv_errors(), true));
        echo "into result fail ";
    }
    else
    {

        while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
        {
         $temp[] = $row;
        }   
    }
    #var_dump($temp, json_encode($temp));
    echo json_encode($temp); 
    sqlsrv_close($ConnectionInfo);

    ?>

I've commented out var_dump but it shows results fine.

var_dump($temp) results below:

array(5) { [0]=> array(2) { ["EntityID"]=> int(466) ["Name"]=> string(20) "A.R.T. Advisors, LLC" } [1]=> array(2) { ["EntityID"]=> int(210) ["Name"]=> string(14) "Abbey National" } [2]=> array(2) { ["EntityID"]=> int(211) ["Name"]=> string(25) "Aberdeen Asset Management" } [3]=> array(2) { ["EntityID"]=> int(209) ["Name"]=> string(25) "ABN AMRO Clearing Bank NV" } [4]=> array(2) { ["EntityID"]=> int(209) ["Name"]=> string(25) "ABN AMRO Clearing Bank NV" } }

SQL query is as below:

$SOLAEntityDetails="
select top 5 en.EntityID, en.Name
from SolaDBServer..tblClientLicense cl
LEFT JOIN tblClientLicenseDetail cd ON cd.ClientLicenseID = cl.ClientLicenseID
LEFT JOIN tblEntity en ON en.EntityID = cl.EntityID
LEFT JOIN tblClientMarketDataContact mdc ON mdc.ClientLicenseID = cl.ClientLicenseID
Order By en.Name asc";
Zulfiqar Dholkawala
  • 458
  • 2
  • 5
  • 19
  • 2
    You need to find out what's wrong with the json operation. https://www.php.net/manual/en/function.json-last-error-msg.php will help you – ADyson Jan 06 '20 at 11:36
  • thanks @ADyson for the tip. I am getting "Malformed UTF-8 characters, possibly incorrectly encoded" while using json_last_error_msg() function.. – Zulfiqar Dholkawala Jan 06 '20 at 12:14
  • 1
    It means you have got some badly-encoded or invalid UTF-8 characters in your data (this can include characters which are normally hidden/invisible). So you likely need to clean your corrupted data, and also [check you are using utf-8 all the way through every part of your application](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through) to prevent issues with encoding in future. – ADyson Jan 06 '20 at 12:16
  • 2
    Does this answer your question? [PHP json encode - Malformed UTF-8 characters, possibly incorrectly encoded](https://stackoverflow.com/questions/46305169/php-json-encode-malformed-utf-8-characters-possibly-incorrectly-encoded) – Rain Jan 06 '20 at 12:17
  • this is clear from MS link here - https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2005/cc626307(v=sql.90)?redirectedfrom=MSDN – Zulfiqar Dholkawala Jan 06 '20 at 13:04

0 Answers0