为什么单引号不可见

时间:2015-09-22 14:12:21

标签: javascript php

我从PHP中的数据库值创建一个数组,然后将其存储到javascript数组中。当我警告数组时,单引号是不可见的,但是当看到PHP时它会显示值的引号。我的代码有什么问题?它应为['1','2','3','4'],但会显示[1,2,3,4]

<script>
    var values = [
        <?php
            $conn = mysql_connect("localhost","root","") or die ("we couldn't connect!");
            mysql_select_db("test_value");
            $rs = mysql_query("SELECT * FROM test") or die(mysql_error());
            while($row = mysql_fetch_array($rs))
            {
                echo "'".$row['var1']."',";
            }
        ?>
    ];
    window.alert(values);
</script>

1 个答案:

答案 0 :(得分:0)

<script>
    var values=[
    <?php
    $conn =mysql_connect("localhost","root","") or die ("we couldn't connect!");
    mysql_select_db("test_value");
    $rs = mysql_query("SELECT * FROM test") or die(mysql_error());
    while($row = mysql_fetch_array($rs)){
            echo "\'".$row['var1']."\',";
    }?>
   ];
    window.alert(values);
    </script>

使用此功能,这可能会对您有所帮助