I'm trying to return the result of a MS SQL Server query as a JSON object.
The query is correct, I also can see the result with echo $row ['name']
, but I don't get any result with echo json_encode ( $arr )
. The page just remains empty.
This is my 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"; // <- 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 ();