I have a similar function that I use for my db connection but it returns the link identifier so I can use it in my queries. Here's the function I use -
function dbconnect(){
// Database connection info
$host = 'localhost';
$database = 'db';
$user = 'user';
$pass = 'pass';
$conn = @mysql_pconnect($host, $user, $pass)
or die('Could not connect to db server.<br>Error: ' . mysql_error());
@mysql_select_db($database)
or die('Could not select db.<br>Error: ' . mysql_error());
return $conn;
}
Then when running queries I do something like -
$link = dbconnect();
$query = "SELECT DATE_FORMAT(timestamp, '%M %D, %Y') AS time FROM login WHERE user='$username'";
$res = mysql_query($query, $link)
or die('MySQL error: ' . mysql_error() . '<br>Query: ' . $query);
Using code like this gives useful error messages when things go "tits up".
Hope this helps 😉