I\'ve read a LOT of things about this problem, however I still cannot fix it.
In my functions file I declare a variable with a value like so:
$px_host =
It's getting late, so this is only a partial answer.
Another approach you could take is to pass the database instance into your helper function, thus resolving the credentials issue.
function dbQuery($database, $reqquery)
{
if (false !== ($query = mysql_query($reqquery, $database))) {
exit("Error - query error.");
}
return $query;
}
Now this function receives its dependency via the arguments and is also a lot shorter and doesn't connect / query / disconnect every time.
The remaining code has to be written elsewhere; if you require a database at every page, you can write this pretty high up the chain:
if (false === ($connect = mysql_connect($px_host, $px_dbuser, $px_dbpass))) {
exit("Error - cannot connect to MySQL server - " . mysql_error());
}
if (false === mysql_select_db($database)) {
exit("Error - cannot select database - " . mysql_error());
}
Then you pass $connect
around wherever it's required.
$res = dbQuery($connect, 'SELECT "hello world"');
Of course, mysql_connect
and friends use implied connections, so you technically don't need to pass it around anyway; it's a good pattern nonetheless.
Last but not least
Learn how to use PDO and revel in the magical world of OOP ;-)