How can I populate a javascript array with values from a database using PHP?

后端 未结 5 885
走了就别回头了
走了就别回头了 2021-01-03 14:59

I have to create a javascript array whose elements are fetched by php from a database. Is it possible? If so, how?

(I dont want to use ajax to do that)

5条回答
  •  天涯浪人
    2021-01-03 15:39

    Collect the values in an array and convert it to JSON with json_encode:

    $array = array();
    while ($row = mysql_fetch_assoc($result)) {
        $array[] = $row['key'];
    }
    echo 'var array = '.json_encode($array).';';
    

提交回复
热议问题