I tried the following code to retrieve values from the database and store them in a javascript array using php array. I tried using the following code, But it is returning me a Reference Error array is not defined.The code is as follows.
<?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>
$indexin yourwhileloop you may as well use$names[] = $row[0].