从数据库获取数据并添加字符

时间:2014-08-09 12:09:12

标签: jquery

这是我正在使用的功能和我正在使用的PHP

  $(function() {
 var availableTags = [
   <?php

     $con=mysqli_connect("localhost","root","","dbnpatient");
      if (mysqli_connect_errno()){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

      $query = mysqli_query($con, "SELECT pname FROM tblnpatient");
    while($row = mysqli_fetch_array($query)){

    echo $row['pname'];


}
        ?>
];
$( "#tags" ).autocomplete({
  source: availableTags
});

});   

我需要的数据将是这样的&#34; John Cruz&#34;,&#34; Jerry Tom&#34;,&#34; Garry Tim&#34;。具有双引号和昏迷的数据

1 个答案:

答案 0 :(得分:0)

您需要通过json_encode函数对结果进行编码,以获得所需的结果:

<?php
    $con=mysqli_connect("localhost","root","","dbnpatient");
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $tags = array();
    $query = mysqli_query($con, "SELECT pname FROM tblnpatient");
    while($row = mysqli_fetch_array($query)){
        $tags[] = $row['pname'];
    }
    $availableTags = json_encode($tags);
?>
<script>
$(function() {
    var availableTags = <?php echo $availableTags;?>;
    $( "#tags" ).autocomplete({
      source: availableTags
    });
}); 
</script>