我尝试使用以下代码从数据库中检索值并使用 php 数组将它们存储在 javascript 数组中。我尝试使用以下代码,但它返回给我一个未定义的引用错误数组。代码如下。
<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cerebra", $con);
$sql="select name from details order by download desc limit 20";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
$query=mysql_query($sql,$con);
$names=array();
$index=0;
while($row=mysql_fetch_array($query)){
$names[$index]=$row[0];
$index++;
}
?>
<script>
var comp=new array();
<?php
$i=0;
foreach($names as $a){
$i++;
echo "comp[$i]='".$a."';\n";
}
?>
for(i=0;i<comp.length;i++)
alert(comp[i]);
</script>