PHP SQLSRV return JSON
<p>I am using a SQL Server database, so I am getting adjusted to the syntax.</p>

<p>I am trying to return some JSON, but I cannot seem to get the formatting correct.</p>

<p>In my PHP script, I have this code below:</p>

<pre><code> &lt;?php
   $select = "SELECT
                CONCAT('\"',COALESCE('',''),'\"') AS EDIT
                ,CONCAT('\"',COALESCE([FOR_PARTNER],''),'\"') AS PARTNER_CODE
                ,CONCAT('\"',COALESCE([FOR_NAME],''),'\"') AS PARTNER_NAME
                ,CONCAT('\"',COALESCE([SHP_PARTNER],''),'\"') AS SHIPPER_PCODE
                ,CONCAT('\"',COALESCE([SHP_NAME],''),'\"') AS SHIPPER_NAME
                ,CONCAT('\"',COALESCE([MODDATE],''),'\"') AS MODDATE
                ,CONCAT('\"',COALESCE([MODUSER],''),'\"') AS MODUSER
                ,CONCAT('\"',COALESCE([ID],''),'\"') AS ID
             FROM
                [main].[dbo].[exceptions]";

 $query = sqlsrv_query($dbc, $select);
 $resnum = sqlsrv_num_rows($query);

 // here is where the error lies

 echo "{\"data\":[";
 $i = 1;    
 while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
 {
   echo "[";
     echo implode(', ', $row);
   echo "]";
   if($i &lt; $resnum){
     echo ",";
   }
   $i++;
 }
 echo "]}";

 sqlsrv_free_stmt($query);
 ?&gt;
</code></pre>

<p>Using the above code, the JSON that is returned looks like this:</p>

<pre><code> {"data":[["", "0000111111", "JBR", "00000999999", "AMERICA INC", "Jan 1 1900 12:00AM", "", "55"]["", "0000777777", "7ALEXANDER LOG", "000222222", "NORRIS INC", "Jan 1 1900 12:00AM", "", "56"]]}
</code></pre>

<p>If you'll notice, there is no comma separating the inner array.  It should be returned like this:</p>

<pre><code> {"data":[["", "0000111111", "JBR", "00000999999", "AMERICA INC", "Jan 1 1900 12:00AM", "", "55"],["", "0000777777", "7ALEXANDER LOG", "000222222", "NORRIS INC", "Jan 1 1900 12:00AM", "", "56"]]} 
</code></pre>

<p>I have used the same syntax with MySQL, but of course, that's MySQL.  I am using SQLSRV above.  I am not sure if the IMPLODE is understood this go around.</p>

<p>So in short, I need to get the comma to separate the inner array.</p>

<p>Does anyone see my error?</p>