variable scope question within a for loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hillxy
Forum Newbie
Posts: 7
Joined: Wed Apr 07, 2004 2:28 pm

variable scope question within a for loop

Post by hillxy »

hi,

how can I access a array whose value was assigned inside a for loop loop

code like
.....
$SQL = "select a_number from table";
$rs = mysql_query($SQL);
$nRows = mysql_num_rows($rs);
for ($i = 0; $i < $nRows; $i++)
{
$k = mysql_fetch_row($rs);
$dc_t0 = $k[0];
echo $dc_t0, " <br>";
}
echo $nRows, $dc_t0[1], "<br>";
.....

inner loop echo works fine, but outter echo command can not see the array.

Any idea?
thanks,
hillxy
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You should be using [$i] not in the loop.
hillxy
Forum Newbie
Posts: 7
Joined: Wed Apr 07, 2004 2:28 pm

Oh, What a stupid mistake I made!!

Post by hillxy »

Thanks,
hillxy
Post Reply