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 ...\" Link to comment https://forums.phpfreaks.com/topic/814-random-query/ 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 Link to comment https://forums.phpfreaks.com/topic/814-random-query/#findComment-2784 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) Link to comment https://forums.phpfreaks.com/topic/814-random-query/#findComment-2787 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 Link to comment https://forums.phpfreaks.com/topic/814-random-query/#findComment-3071 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.