Hello i’m trying to make a tag script for my website so each time a search engine comes to my website 10 different tags will show on my website.
These tags will be grabbed from the db.
So at the minute i have coded it so it grabs only one. ( because i don’t know how to do a while )
Like so
$sql = "SELECT tagname FROM tags ORDER BY rand() LIMIT 10";
$result = mysql_query($sql);
$row = mysql_fetch_object($result);
echo "<a href='index.php'>" .$row->tagname. " </a>";
Is there anyway i can add a while to that so it does it 10 times ? E.g use the same echo but print out 10 results instead of the 1 …. i have changed the limit from 1 to 10 but that did not work… still showing one…
Note, read before the real answer: for the ones that keep downvoting this answer. Read the title (that starts with “Doing a while“) and the final part, the question (“Is there anyway i can add a while to that so it does it 10 times ?”). This answer is about iterating the result set, not about the usage of the RAND function! The query doesn’t even appear in my answer, and I am also suggesting a different approach at the end:
you just need to wrap your call to mysql_fetch_object in a loop
Later edit
Other considerations would be:
you should have some error handling even if the query seems to be
perfect, at least
if (!$result)
{
echo mysql_error();
die;
}