Explanation of For each loop
Posted: Thu Nov 12, 2009 4:48 pm
Hi all,
I have a misunderstanding regarding foreach loops which I can't resolve, nor can I figure out how to get the code right to do what I want.
First I'm storing the results of a Mysql query into an array.
The results contain multiple rows, each row has several that pertain to it-- site_id, site_name, etc.
I confirmed that results of the query do exist each with a value in site_id
For each row that was returned I want to run a process, but I get the following messages:
Undefined index: site_id
Invalid argument supplied for foreach()
foreach ($ftp_sites['site_id'] as $key=>$val)
{
//// CODE
}
Can someone explain why $ftp_sites['site_id'] isnt being recognized and /or what approach I should take to resolve
I have a misunderstanding regarding foreach loops which I can't resolve, nor can I figure out how to get the code right to do what I want.
First I'm storing the results of a Mysql query into an array.
The results contain multiple rows, each row has several that pertain to it-- site_id, site_name, etc.
Code: Select all
// Store all sites to ftp into an array
$ftp_sites = Array();
$query = 'SELECT *
FROM `'. LOCAL_SITES .'`
WHERE ftp = 1';
$result = mysql_query( $query ) or die(mysql_error());
while( $row = mysql_fetch_array( $result ) )
{
$ftp_sites[] = $row;
}
For each row that was returned I want to run a process, but I get the following messages:
Undefined index: site_id
Invalid argument supplied for foreach()
foreach ($ftp_sites['site_id'] as $key=>$val)
{
//// CODE
}
Can someone explain why $ftp_sites['site_id'] isnt being recognized and /or what approach I should take to resolve