Question:
How to echo a different variable value each pass in a WHILE loop in (PHP)?
peter s
2006-11-06 08:19:32 UTC
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.
Three answers:
Michael T
2006-11-06 09:01:02 UTC
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";

}
?
2016-10-21 12:52:33 UTC
The mutually as loop will loop until eventually the difficulty is genuine and because your assigning a variable a value on your mutually as loop it is going to consistently be genuine. you could desire to interchange your = with == on your mutually as loop. = gadgets, == compares :)
JTTech
2006-11-06 08:26:47 UTC
You're missing some code $NAME isn't in the extraction code.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...