kris1988edwards Posted December 3, 2014 Share Posted December 3, 2014 Good morning, I am trying to convert a mssql query into json format so that I can then later pass this through google's visualisation api. The query and encoding seems to be working but the encode returns NULL. I have checked the normal gotcha's of making sure its utf8 encoded and that I have used a version of PHP that has the encode (using php 5.3.19). Can any one help me with getting the encode to work. PHP CODE: <?php //connection and database details (known to be working) if( $conn ) { echo "Connection established.<br><br>"; }else{ echo "Connection could not be established.<br />"; die( print_r( sqlsrv_errors(), true)); } $query = "SELECT * FROM tblProducts"; $result = sqlsrv_query( $conn, $query); while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ); $arr = array($result); $encodedarray = array_map(utf8_encode, $arr); echo json_encode($encodedarray); sqlsrv_close( $conn); ?> When I run this code on the server it comes back with: Connection established.[null] Has anyone got any ideas of getting this to work? Thanks Kris Quote Link to comment https://forums.phpfreaks.com/topic/292861-php-json_encode-null-issues/ Share on other sites More sharing options...
Solution Barand Posted December 3, 2014 Solution Share Posted December 3, 2014 The only thing that gets executed in your while loop is the bit between while(..) and the ";" at the end of that line. In other words - nothing. try while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) ) { $encodedarray = array_map(utf8_encode, $row); echo json_encode($encodedarray); } Quote Link to comment https://forums.phpfreaks.com/topic/292861-php-json_encode-null-issues/#findComment-1498351 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.