dte66654 2014-12-03 11:06
浏览 43
已采纳

Php / json_encode NULL问题

Good morning,

I have already read the other posts that have similar issues, but nothing is resolving the issue, but bear in mind I am new to the world of PHP and Json. But 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 details known to be working fine

if( $conn ) {
echo "Connection established.<br><br>";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}

$query = "SELECT * FROM tblMetalPrice";
$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

  • 写回答

2条回答 默认 最新

  • douyi1939 2014-12-03 11:30
    关注

    The way I resolved this was:

    $query = "SELECT * FROM tblMetalPrice";
    $result = sqlsrv_query( $conn, $query);
    while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC) )
    
    {
    
    $encodedarray = array_map(utf8_encode, $row);
    
    echo json_encode($encodedarray);
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分