Jump to content

Php / json_encode NULL issues


kris1988edwards

Recommended Posts

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 
Link to comment
https://forums.phpfreaks.com/topic/292861-php-json_encode-null-issues/
Share on other sites

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);

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.