0

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

5
  • 1
    what var_dump($return_arr); returns? Commented Apr 24, 2014 at 22:50
  • I think you mean ["Integra Fibre Unlimited",null,"Integra Professional Web Hosting"]. Commented Apr 24, 2014 at 22:50
  • SELECT * FROM customer_billing WHERE productname IS NOT NULL GROUP BY productname;? Commented Apr 24, 2014 at 22:51
  • the productname for the second row is not null anyway Commented Apr 24, 2014 at 22:52
  • @SharikovVladislav it shows - "Integra Fibre Unlimited" [91]=> string(37) "Integra Fibre Unlimited (RRP: £59.95)" [92]=> string(32) "Integra Professional Web Hosting" Commented Apr 24, 2014 at 22:52

1 Answer 1

1

Try:

// some code
while($result=mysql_fetch_array($rs)) {
    $return_arr[] =  utf8_encode($result["productname"]);
}
echo utf8_decode(json_encode($response)); 
Sign up to request clarification or add additional context in comments.

1 Comment

Wow. Rly? :D Need explanation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.