Jump to content

Php / json_encode NULL issues


Go to solution Solved by Barand,

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

  • Solution

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

}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.