Hi there

I'm trying to cycle through entries inside a MySQL db and create numbered variables inside a for loop. Currently it doesn't seem to be working.

Here's an example of what I'm trying to do:

$i = 0;
while($row = mysql_fetch_row($result))
{
	$i++;
	$variable.$i = $row[0];
}

echo $variable1;
echo $variable2;
echo $variable3;

Any help would be much appreciated ;)

$var = $variable.$i;
$$var = $row[0];

I'm not entirely sure why you would want to do this instead of use an array though.

$var = $variable.$i;
$$var = $row[0];

I'm not entirely sure why you would want to do this instead of use an array though.

I was just fiddling with the idea of an array. But the code I have doesn't seem to be working properly.

$variable = array();
while($row = mysql_fetch_row($result))
{
	$variable += $row[0];
}

echo $variable[0];
echo $variable[1];
echo $variable[2];

Having the plus in front of the equal sign doesn't work and removing it only provides a partial bit of the first entry in the database. Very confusing.

$variable[]=$row[0];

Arrays use the []= operator, not +=

commented: A great help ;) +2
$variable[]=$row[0];

Arrays use the []= operator, not +=

Aaaah perfect. Was just busy reading up on array_push() but your answer is doing the trick. Thanks ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.