0

I'm building an ad system and I would like to know what's the best way to randomise the display of banners? I'm using PHP and MySQL and I'm not happy with MySQL's RAND().

Psyche
  • 8,513
  • 20
  • 70
  • 85

1 Answers1

0

It's best to use the MySQLs' functions to do all the randomizing for you... But if your "not happy" for reasons unspecified.. You can use a bulkier method before gathering your data from the database:

$Query = $DB->prepare("SELECT ID FROM Tablename ORDER BY ID DESC LIMIT 1"); 
$Query->execute();
$Query->bind_result($MaxID);
$Query->fetch();
$Query->close(); 

$Random_ID = rand(0,$MaxID);
unset($Query);
$Query = $DB->prepare("/* Select your random banner WHERE ID=".$Random_ID."*/");
$Query->execute();
$Query->bind_result(/*Results to display*/);
$Query->fetch(); 
$Query->close(); 



// Continue to display the data pulled from the query

For this Example, I have used MySQLi... But as you can see, it's a longer process to use PHP for example to grab random data..

So long story short... It's best to use MySQL to obtain your random data

Daryl Gill
  • 5,464
  • 9
  • 36
  • 69