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.

 

 

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'];

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.