Jump to content

Random selection from a database.


NextGenForum.net

Recommended Posts

Hi,

 

I run a site - www.selectgreen.co.uk and as you can see there is a section called featured company. I want to pull out a random entry from about 11 databses altogether in order for it to show up as a featured company everyday.

 

I dont know if this is possible, but help appreciated (hope im clear here  ;D)

 

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/74886-random-selection-from-a-database/
Share on other sites

I mean that every day, i want a different company to appear. I have 11 databases one for each category (property, music etc...) So i need to somehow pull out an entry from one of these databases to appear in the featured company box.

 

i probably havent made my self any clearer, but thats the best i can explain it. sorry

If so, then get back min/max of the uid field for the table, and pick one "in the middle":

 

$range_result = mysql_query( " SELECT MAX(`id`) AS max_id , MIN(`id`) AS min_id FROM `table` ");
$range_row = mysql_fetch_object( $range_result );
$random = mt_rand( $range_row->min_id , $range_row->max_id );
$result = mysql_query( " SELECT * FROM `table` WHERE `id` >= $random LIMIT 0,1 ");

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.