This is a function that I want to put on my website to display ads. The problem is explained in the code's comments.
<?php
//The function that calls the ads
function ShowAd($position = ""){
$dbusername = "####";
$dbhost = "####";
$dbpassword = "####";
$dbname = "####";
$dbconnect = mysql_connect($dbhost,$dbusername,$dbpassword) or die('I cannot connect to the database. ' . mysql_error());
$db = mysql_select_db($dbname,$dbconnect) or die('I cannot select the database. ' . mysql_error());
$query = "SELECT * FROM igo_ads WHERE position = '$position'";
$result = mysql_query($query);
$bannercheck = mysql_num_rows($result);
//If none, the page continues to load without any ad
if($bannercheck == 0){
}
//If there is one, it's loaded
elseif($bannercheck == 1){
while($ad = mysql_fetch_array($result)){
//Here is the problem, those vars are empty. It seems that the mysql_fetch_array() doesn't return anything...
//I know that the scipt reaches this place because I put "echo $bannercheck;" and it displayed 1
//I also tried if(!isset($id){
//echo "The var is empty";
//}
//and it did show that the var is empty..
$id = $ad[id]; //ID
$image = $ad[image]; //Image URL
$name = $ad[name]; //Site Name
//Displaying the ad, when you click the ad a hit is counted and you are redirected to the site (that part works)
echo "<a href='adclick.php?id=$id' target='_blank'><img src='$image' border='0' alt='$name'></a>";
}
}
//If there is more than one result, which shouldn't happpen, this is displayed
else{
echo "There is a problem in the ad database";
}
}
?>
What do I do wrong?