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数组将它们存储在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>
4 个解决方案
#1
1
It is var comp = new Array()
not array()
. Skip that anyhow and use var comp = []
right away.
它是var comp = new Array()而不是Array()。不管怎么说,直接使用var comp =[]。
#2
1
I think you're going about this the wrong way. Here is a much better way of going about it:
我认为你走错路了。这里有一个更好的方法:
<?php
$phpArray = array("foo", "bar", "baz");
//....
?>
<script type="text/javascript">
var jsArray = <? echo json_encode($phpArray); ?>;
</script>
Taken from here: How to use an array value from php to javascript?
从这里开始:如何使用从php到javascript的数组值?
#3
1
new array();
should be
应该是
new Array();
in your javascript.
在你的javascript。
EDIT: you should also stop using mysql_* functions.
编辑:您也应该停止使用mysql_*函数。
#4
1
First of all, why do you run your query twice?
首先,为什么要运行两次查询?
...
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
$query=mysql_query($sql,$con);
...
Second in Javascript the array object is called Array
, not array
. So try with
在Javascript中,数组对象被称为数组,而不是数组。所以试着
var comp = new Array();
Update:
更新:
but the first value am getting is undefined. How come? – user2129868
但是第一个值是未定义的。如何来吗?——user2129868
Because you increment $i
before getting the value. So $i
is 1
and not 0
on your first iteration in your Javascript.
因为你在得到值之前增加$i。所以$i是1,而不是0。
So change
所以改变
<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>
to
来
<script>
var comp=new array();
<?php
$i=0;
foreach($names as $a){
echo "comp[".$i++."]='".$a."';\n";
}
?>
for(i=0;i<comp.length;i++)
alert(comp[i]);
</script>
Otherwise when you try to fetch the elements with
否则当你尝试去获取元素的时候。
for(i=0;i<comp.length;i++)
alert(comp[i]);
0
will be undefined since 1
was the first index you added.
0将没有定义,因为1是您添加的第一个索引。
#1
1
It is var comp = new Array()
not array()
. Skip that anyhow and use var comp = []
right away.
它是var comp = new Array()而不是Array()。不管怎么说,直接使用var comp =[]。
#2
1
I think you're going about this the wrong way. Here is a much better way of going about it:
我认为你走错路了。这里有一个更好的方法:
<?php
$phpArray = array("foo", "bar", "baz");
//....
?>
<script type="text/javascript">
var jsArray = <? echo json_encode($phpArray); ?>;
</script>
Taken from here: How to use an array value from php to javascript?
从这里开始:如何使用从php到javascript的数组值?
#3
1
new array();
should be
应该是
new Array();
in your javascript.
在你的javascript。
EDIT: you should also stop using mysql_* functions.
编辑:您也应该停止使用mysql_*函数。
#4
1
First of all, why do you run your query twice?
首先,为什么要运行两次查询?
...
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
$query=mysql_query($sql,$con);
...
Second in Javascript the array object is called Array
, not array
. So try with
在Javascript中,数组对象被称为数组,而不是数组。所以试着
var comp = new Array();
Update:
更新:
but the first value am getting is undefined. How come? – user2129868
但是第一个值是未定义的。如何来吗?——user2129868
Because you increment $i
before getting the value. So $i
is 1
and not 0
on your first iteration in your Javascript.
因为你在得到值之前增加$i。所以$i是1,而不是0。
So change
所以改变
<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>
to
来
<script>
var comp=new array();
<?php
$i=0;
foreach($names as $a){
echo "comp[".$i++."]='".$a."';\n";
}
?>
for(i=0;i<comp.length;i++)
alert(comp[i]);
</script>
Otherwise when you try to fetch the elements with
否则当你尝试去获取元素的时候。
for(i=0;i<comp.length;i++)
alert(comp[i]);
0
will be undefined since 1
was the first index you added.
0将没有定义,因为1是您添加的第一个索引。