My problem (and question) is how the DB connection works.
When using the vars as shown, the function always returns
with No Database Selected
However if filling in the values in place of the vars in
mysql_connect and mysql_select_db
then the connection always returns as true.
With functions is there a certain way needed to make
vars for things like a db conn to work correctly?
Because I really dont want to have the true values for
db info in the function if not needed.
Code: Select all
$dbhost = "localhost";
$dbuser = "user";
$dbpass = "password";
$dbname = "dbname";
function site_banner()
{
mysql_connect($dbhost, $dbuser, $dbpass) or die("Couldn't connect to database server: " . mysql_error());
mysql_select_db($dbname) or die("Couldn't connect to database: " . mysql_error());
$result = mysql_query("SELECT * FROM Banner ORDER BY RAND() LIMIT 1");
while ($row = mysql_fetch_assoc($result)){
echo $row['image'];
}
}