tags:

views:

93

answers:

4
$child= array();
$i = 0;

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    $child[i] = $row['userId'];
    $i++;
} 

$i = 0;
while($i<=5)
{
    echo $child[i];
    $i++;
}

It is printing same value.

+7  A: 

You are using i for the array index instead of $i:

$child[i]

This should be $child[$i]. Because the i in $child[i] is interpreted as a constant (value of constant i is used as index) or if there is no such constant as string. You can get a variable/value dump with var_dump.

Gumbo
+1. Whatever happened to the good ole' days where the 'first correct answer gets the vote'.
karim79
I don't know what happened to those days, I didn't vote on myself. Maybe people liked the smiley face at the end, I myself think this answer is better formatted (and posted 1 minute before mine) so should be on top.
CharlesLeaf
And yet another reason why you should test with the E_NOTICE error level set...
ircmaxell
+6  A: 

you forgot an $i in $child[i].. should be $child[$i] ;)

CharlesLeaf
+1  A: 
$child= array();
$i = 0;

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    $child[$i] = $row['userId'];
    $i++;
} 

 $i = 0;
while($i<=5)
{
    echo $child[$i];
    $i++;
}

You weren't calling $i, but "i"

Powertieke
+1  A: 

If you want numeric indexes you can do without $i .. try ..

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    $child[] = $row['userId'];
} 

for($i=0;$i<5;$i++)
{    
echo $child[$i].' <br />;
}

Hope it helps.

Wbdvlpr