PHP json_encode显示null

时间:2014-04-24 22:48:12

标签: javascript php sql

我在PHP中运行SQL查询并将值放入JS变量:

<?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>

我有3行,列productname等于:

  • Integra Fiber Unlimited
  • Integra Fiber Unlimited(建议零售价:59.95英镑)
  • Integra专业虚拟主机

当我使用echo json_encode($return_arr);时,它显示为:

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

它只是不喜欢显示第二个

1 个答案:

答案 0 :(得分:1)

尝试:

// some code
while($result=mysql_fetch_array($rs)) {
    $return_arr[] =  utf8_encode($result["productname"]);
}
echo utf8_decode(json_encode($response));