Deviant Logo

How to echo a different variable value each pass in a WHILE loop in (PHP)?

post details top
May 10th, 2010
post details top


This script echos 70 variables values named $NAME from the database randomly. But it shows the same random name that was picked and echoes the same name 70 times e.g. TONYTONYTONY…. Is it possible to echo 70 different NAMES using a while loop so that one name never repeats itself twice. E.g. TONY ROBERT CHARLEY JAMES CHRIS. You get the idea.

This is the script i used that echos 70 of the same name:

$query = “SELECT * FROM tablename ORDER BY rand() LIMIT 70″;
$result = mysql_query($query)
or die (“crap”);
$num = mysql_num_rows($result);
$row = mysql_fetch_array($result);
extract($row);
while ($row = mysql_fetch_array($result))
{
echo “$NAME”;
}

Can you edit this to make it echo a different name each pass of the while loop and never repeat more than once.

Thanx, any questions just ask. I need help bad lol.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

2 Comments

  • JTTech

    You’re missing some code $NAME isn’t in the extraction code.

  • Michael T

    Try this, just change the database reference and set the table field name as you have:

    $dbname=”database_name”;
    $result=MYSQL($dbname,”SELECT * FROM tablename ORDER BY rand() LIMIT 70″);
    while ($row=mysql_fetch_array($result))
    {
    $NAME = $row["NAME"];
    echo “$NAME”;
    }

Leave a Reply

Search

  • Partner links