Jump to content

RAndom Query


jabbamonkey

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

Link to comment
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)

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.