Jump to content

[SOLVED] whats the best way to display a random record from a mysql table?


daveh33

Recommended Posts

What I am trying to do is do a query on a mysql table e.g SELECT * from table where status='active' - I want it so when e.g 10 records are returned, it would display a randomly selected.

 

I have spent a few hours searching the internet for tutorials but have yet to get one working.

 

I have also noticed different ways in doing it.

 

 

Can someone let me know some methods which have been successfully used before.

 

 

Link to comment
Share on other sites

Can you tell me where I am going wrong with this code

 

} else {
$result = mysql_fetch_row(mysql_query("SELECT * from promotions where used='n' ORDER BY RAND(id)")  or die(mysql_error()));
$row = mysql_fetch_array( $result2 ) or die(mysql_error());
} 
$title = $row['title'];

Link to comment
Share on other sites

change line

$result = mysql_fetch_row(mysql_query("SELECT * from promotions where used='n' ORDER BY RAND(id)")  or die(mysql_error()));

to

$result = mysql_query("SELECT * from promotions where used='n' ORDER BY RAND()")  or die(mysql_error());

Link to comment
Share on other sites

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.