Jump to content

Order by Random


JackW

Recommended Posts

I am stumped. I want my site to display the information from the data base in a truly random order. I know how to write it to display in ASC (ascending) or DESC (descending) order but I want it to come up in a different order each time it displays. Is that possible? I have 30 rows of sponsors in the data base and want to display them 15 at a time but not in any specific order.It sounds simple but I can’t seem to find anything about it.

Link to comment
Share on other sites

Thanks,Not sure I understand what you said. Here is what I have and it works great, but not random.Can I replace DESC with something that will make it random? I tried removing order by and it works but comes up in the same order most of the time.

@ $db = mysql_pconnect('myserver','mydatabase','mypassword');mysql_select_db('mydatabase') or die( "Unable to select database");$query= 'SELECT * FROM `sponsors` order by `ID` DESC LIMIT 0, 15 ';$query_results=mysql_query($query);$match_results=mysql_num_rows($query_results);for ($i=0; $i < $match_results; $i++){$row=mysql_fetch_array($query_results);echo '<a href="';echo($row ['url'] ), '"', '>';echo($row ['name'] ), '</a><br>';echo($row ['comment'] ), '<br><br>';}mysql_close($db);

Link to comment
Share on other sites

Thank you both. That did it. The new code is below and works like a charm.

@ $db = mysql_pconnect('myserver','mydatabase','mypassword');mysql_select_db('mydatabase') or die( "Unable to select database");$query= 'SELECT * FROM `sponsors` order by RAND() LIMIT 0, 15 ';$query_results=mysql_query($query);$match_results=mysql_num_rows($query_results);for ($i=0; $i < $match_results; $i++){$row=mysql_fetch_array($query_results);echo '<a href="';echo($row ['url'] ), '"', '>';echo($row ['name'] ), '</a><br>';echo($row ['comment'] ), '<br><br>';}mysql_close($db);

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...