I have a much more complex problem that I am trying to resolve, however it all comes down to getting mysql_fetch_array values out of a while loop so I can use the values for comparison somewhere else.
我有一个更复杂的问题,我试图解决,但这一切都归结为从while循环中获取mysql_fetch_array值,所以我可以在其他地方使用这些值进行比较。
For example:
例如:
$result = mysql_query("SELECT name FROM Names");
while ($data = mysql_fetch_array($result))
{
$val = $data['name'];
//let's say there are 5 names here//
$array1 = array($val);
}
//now, I would like to use the five names outside of the loop//
I do not understand how to get all five names out of this loop. I have them in an array, but when I print the array from outside the loop, all I get is the last record. If I print the array from within the Loop, I get all records.
我不明白如何从这个循环中取出所有五个名字。我把它们放在一个数组中,但是当我从循环外部打印数组时,我得到的只是最后一条记录。如果我从Loop中打印数组,我会得到所有记录。
Is it possible to store the values in the loop, move them from the loop, and then use them somewhere else?
是否可以将值存储在循环中,将它们从循环中移出,然后在其他地方使用它们?
4 个解决方案
#1
3
You could do somthing like this:
你可以这样做:
$result = mysql_query("SELECT name FROM Names");
$dataset = array();
while ($data = mysql_fetch_array($result))
{
$dataset[] = $data;
}
Then you are free to use $dataset
outside the loop...
然后你可以在循环外使用$ dataset ...
#2
3
$array1 = array();
$result = mysql_query("SELECT name FROM Names");
while ($data = mysql_fetch_array($result)){
$val = $data['name'];
//let's say there are 5 names here//
$array1[] = $val;
}
#3
2
You are setting $array1
every time in the loop. You need to ADD $val
to the array, not set the array to $val
.
你每次都在循环中设置$ array1。您需要向数组添加$ val,而不是将数组设置为$ val。
$result = mysql_query("SELECT name FROM Names");
while ($data = mysql_fetch_array($result))
{
$val = $data['name'];
$array1[] = array($val);
}
Notice the $array1[] =
instead of $array1 =
.
注意$ array1 [] =而不是$ array1 =。
#4
0
You are overwriting the array. Everytime the loop execute it overwrites the value, so you get only the last value. Try
你正在覆盖数组。每次循环执行时都会覆盖该值,因此您只获得最后一个值。尝试
$result = mysql_query("SELECT name FROM Names");
$i = 0;
while ($data = mysql_fetch_array($result))
$val = $data['name'];
$array1[$i] = $val;
$i++;
}
#1
3
You could do somthing like this:
你可以这样做:
$result = mysql_query("SELECT name FROM Names");
$dataset = array();
while ($data = mysql_fetch_array($result))
{
$dataset[] = $data;
}
Then you are free to use $dataset
outside the loop...
然后你可以在循环外使用$ dataset ...
#2
3
$array1 = array();
$result = mysql_query("SELECT name FROM Names");
while ($data = mysql_fetch_array($result)){
$val = $data['name'];
//let's say there are 5 names here//
$array1[] = $val;
}
#3
2
You are setting $array1
every time in the loop. You need to ADD $val
to the array, not set the array to $val
.
你每次都在循环中设置$ array1。您需要向数组添加$ val,而不是将数组设置为$ val。
$result = mysql_query("SELECT name FROM Names");
while ($data = mysql_fetch_array($result))
{
$val = $data['name'];
$array1[] = array($val);
}
Notice the $array1[] =
instead of $array1 =
.
注意$ array1 [] =而不是$ array1 =。
#4
0
You are overwriting the array. Everytime the loop execute it overwrites the value, so you get only the last value. Try
你正在覆盖数组。每次循环执行时都会覆盖该值,因此您只获得最后一个值。尝试
$result = mysql_query("SELECT name FROM Names");
$i = 0;
while ($data = mysql_fetch_array($result))
$val = $data['name'];
$array1[$i] = $val;
$i++;
}