dongwu8064 2014-04-24 22:48
浏览 58
已采纳

PHP json_encode显示null

I am running an SQL Query in PHP and putting the values into a JS variable:

<?php
$return_arr = array();
$sql="SELECT * from customer_billing group by productname ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs)) {
    $return_arr[] =  $result["productname"];
}
echo json_encode($return_arr);
?>
<script type="text/javascript">
$(function() {
    var availableTags = <?php echo json_encode($return_arr); ?>
    //autocomplete
    $(".auto").autocomplete({
        source: availableTags
    });             
});
</script>

I have 3 rows with the column productname equal to:

  • Integra Fibre Unlimited
  • Integra Fibre Unlimited (RRP: £59.95)
  • Integra Professional Web Hosting

and when i use echo json_encode($return_arr);, it displays like:

"Integra Fibre Unlimited",null,"Integra Professional Web Hosting"

it just doesn't like displaying the second one

  • 写回答

1条回答 默认 最新

  • dsf5632 2014-04-24 22:56
    关注

    Try:

    // some code
    while($result=mysql_fetch_array($rs)) {
        $return_arr[] =  utf8_encode($result["productname"]);
    }
    echo utf8_decode(json_encode($response)); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效