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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记