Jump to content

Archived

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

jabbamonkey

RAndom Query

Recommended Posts

I have a list of items, and I want to choose one randomly to appear on the page. Is there a simple way, while writing a query, to choose 1 random row from a table? If so, can someone tell me how?

 

I know I can do it using the following steps, but want to know if there is a shortcut, simply doing it within the query ...

1) Count rows in a table (lets say the number of rows is 234)

2) Choose a random number ($rand_row is a number between 1 and 234)

3) Now, perform a query with \"... LIMIT $rand_row, 1 ...\"

Share this post


Link to post
Share on other sites
) Count rows in a table (lets say the number of rows is 234)

 

use mysql_rows() function

 

2) Choose a random number ($rand_row is a number between 1 and 234)

As said by rhysmeister use the rand() function

 

syntax

rand (int min, int max)

 

3) Now, perform a query with \"... LIMIT $rand_row, 1 ...\"

 

$random = ran(1,234);



$sql = "select * from table limit  1,$random"rand (int min, int max)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.