Jump to content


Photo

RAndom Query


  • Please log in to reply
3 replies to this topic

#1 jabbamonkey

jabbamonkey
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 01 August 2003 - 05:03 PM

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 ...\"

#2 rhysmeister

rhysmeister
  • Members
  • PipPipPip
  • Advanced Member
  • 51 posts
  • LocationEngland

Posted 06 August 2003 - 08:27 AM

Use the
rand()
function.

http://uk.php.net/ma...nction.rand.php

#3 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 06 August 2003 - 09:07 AM

) 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)

Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#4 grumm3t

grumm3t
  • Members
  • Pip
  • Newbie
  • 2 posts
  • LocationHouston, TX, USA

Posted 20 August 2003 - 05:02 AM

SELECT * FROM <tablename> ORDER BY RAND() LIMIT 1




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users