Help with require() and variables
Posted: Sat Jul 14, 2007 2:56 pm
Hi!
I have the following problem:
I am including two files, one of which is a DB link, the other is the DB configuration. The DB link variable somehow never gets recognized though.
The usernames and passwords are correct. When I try to run the db_connect() function, though, I get this error message:
It seems to me that the include doesn't work correctly.
If I take the mysql_connect() function out of the db_connect() function, the connection works, but somehow the $db variable is never recognized: When db_disconnect() is called, it tells me that it's not a valid database link.
What am I doing wrong? Am I missing something about require/include?
/index.php
/include/config.php
/include/db_functions.php
I have the following problem:
I am including two files, one of which is a DB link, the other is the DB configuration. The DB link variable somehow never gets recognized though.
The usernames and passwords are correct. When I try to run the db_connect() function, though, I get this error message:
Code: Select all
Warning: mysql_connect(): Access denied for user: 'www-data@localhost' (Using password: NO) in /www/htdocs/ludo/include/db_functions.php on line 7
Could not connect: Access denied for user: 'www-data@localhost' (Using password: NO)If I take the mysql_connect() function out of the db_connect() function, the connection works, but somehow the $db variable is never recognized: When db_disconnect() is called, it tells me that it's not a valid database link.
What am I doing wrong? Am I missing something about require/include?
/index.php
Code: Select all
<?php
require("include/db_functions.php");
db_connect();
db_disconnect();
?>Code: Select all
<?php
global $db_host;
global $db_name;
global $db_user;
global $db_pass;
global $db_lock;
// Database settings
$db_host = "localhost";
$db_name = "dbname";
$db_user = "username";
$db_pass = "password";
// Locking Changes
$db_lock = FALSE;
?>Code: Select all
<?php
require_once("config.php");
global $db;
function db_connect() {
$db = mysql_connect($db_host, $db_user, $db_pass)
or die ("Could not connect: " . mysql_error());
mysql_select_db($db_name) or die ("Could not select database {$db_name}!");
}
function db_disconnect() {
mysql_close($db);
}
?>