jabbamonkey Posted August 1, 2003 Share Posted August 1, 2003 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 ...\" Quote Link to comment Share on other sites More sharing options...
rhysmeister Posted August 6, 2003 Share Posted August 6, 2003 Use the rand() function. http://uk.php.net/manual/en/function.rand.php Quote Link to comment Share on other sites More sharing options...
shivabharat Posted August 6, 2003 Share Posted August 6, 2003 ) 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) Quote Link to comment Share on other sites More sharing options...
grumm3t Posted August 20, 2003 Share Posted August 20, 2003 SELECT * FROM <tablename> ORDER BY RAND() LIMIT 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.