Random banners from a database
In this example we will show you a simple , no frills way to have a banner rotation system with information from A MySQL database . First we will build our database called banners.

Now we shall create our table called banners , this will contain three fields id , image and url . image will contain our images and url will be the url that you will go to if you click on the banner.

Now lets add some banner information , for this example you will have to create your own banners . Here you can see some information we have entered.

Now we are ready for the script to generate our random banner
<?php
//your hostname , username , password and the database
$hostname = "localhost";
$username = "myusername";
$password = "mypassword";
$database = "banners";
//store connection info in $connect variable
$connect = mysql_connect($hostname , $username , $password)
or die("cannot make connection");
//connect to the database using our $connect
$db = mysql_selectdb($database , $connect);
//select all from banners table
$sql_query = "SELECT * FROM banners";
$result = mysql_query($sql_query);
//find the number of rows
$countrows = mysql_num_rows($result);
//generate a random number
srand(time());
$random = (rand()% $countrows + 1);
//select the id column that matches $random
$sql_query2 = "SELECT * FROM banners WHERE id = $random";
$result2 = mysql_query($sql_query2);
while ($rows = mysql_fetch_array($result2))
{
//display url and image for the banner
echo "<a href = '$rows[2] '><img src =' $rows[1] '>";
echo "";
}
?>