Return JSON after MS SQL Server query
<p>I'm trying to return the result of a MS SQL Server query as a JSON object.</p>

<p>The query is correct, I also can see the result with <code>echo $row ['name']</code>, but I don't get any result with <code>echo json_encode ( $arr )</code>. The page just remains empty.</p>

<p>This is my code:</p>

<pre><code>$sql = "SELECT * FROM tab1";

    $stmt = sqlsrv_query ( $conn, $sql );
    if ($stmt === false) {
        die ( print_r ( sqlsrv_errors (), true ) );
    }

    $arr = array ();
    while ( $row = sqlsrv_fetch_array ( $stmt ) ) {
        // echo $row ['name'] . "\n";  // &lt;- this works
        array_push ( $arr, $row );
    }

    echo json_encode ( $arr );

    sqlsrv_free_stmt ( $stmt );
    sqlsrv_close ( $conn );

    header ( "Content-type: application/json; charset=utf-8" );
    die ( json_encode ( $arr ) );
    exit ();
</code></pre>