Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get array outside of while loop?

Status
Not open for further replies.

Recordsetclown

Technical User
Jul 12, 2002
64
0
0
US
I'm new to PHP programming and am having trouble with variable scope. If I echo the value of $data within the following loop, I get the expected values. How can I transfer those values to an array that I can access outside the loop?

while ($data = mysql_fetch_array($result)) {
for ($i=0; $i<$numcols; $i++) {
echo $data[$i];
}
}
 
Not sure what you are asking, the loop is necessary so that you can move through the rows returned by the query.

Then each row becomes an array as you are stepping through them.

You can of course just output everything into a larger array but I don't see the point.

Perhaps you can explain what exactly it is you want to accomplish.

If you want to be able to move around the results, you can use mysql_data_seek to move the pointer around, and then just issue a single mysql_fetch_array to get the current row.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I need to save the results outside the while loop somehow, but $data[$i] apparently retains no value once while is complete.
 
Code:
while ($data[] = mysql_fetch_assoc($result)){
//do nothing
}

$data will now contain an array of associative arrays from your recordset, in the order that your query delivers them.

Code:
print_r($data);
 
Why can't you save them while in the While loop?
Or since you are using mysql_fetch_array, you cna use the column or field names instead of the for loop to address the columns of each row:

$data['columnname']










----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi

Recordsetclown said:
I need to save the results outside the while loop somehow, but $data[$i] apparently retains no value once while is complete.
It does retain everything assigned to it inside the [tt]while[/tt].

Your problem is that, when [tt]while[/tt] finds out that no more loops will be done, the [tt]false[/tt] returned by [tt]mysql_fetch_array()[/tt] is already assigned to $data.

jpadie's suggested code will clearly confirm this, as $data will contain [tt]mysql_num_rows()[/tt]+1 items, the last being a single [tt]false[/tt] value.

Feherke.
 
Thanks very much folks. As Feherke says, jpadie's code showed me the problem.

When I ran my code, I saw numbers 2 through 7, which are from a mysql column named userdb_id. I would either test the value of $data[2] outside the loop or assign the value of $data[$i] to another array within the loop and test $anothervariable[2] outside the loop, expecting the number echoed in the third iteration of $i, but getting nothing, and thinking that was a scope issue.

Running jpadie's code showed that $data[0] is itself an array containing ['userdb_id'][0 through 5]. Referencing the array as ['userdb_id'][number 0 through 5] gives the value of that userdb_id.

Thanks again to all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top