Saturday, 31 August 2013

PHP Error: Constants get undefined after including them

PHP Error: Constants get undefined after including them

Ok heres the problem. I defined a few constants to store in my database
information.
Example -
FileName: config.php
<?php
//Define Database Constants
defined("DB_SERVER") ? NULL : define("DB_SERVER", "localhost");
defined("DB_NAME") ? NULL : define("DB_NAME", "test");
defined("DB_USER") ? NULL : define("DB_USER", "root");
defined("DB_PASSWORD") ? NULL : define("DB_PASSWORD", "password");
?>
I've included this in another file called database.php :
<?php
require_once("config.php");
class MySQLDatabase {
private $connection;
function __construct(){
$this->open_connection();
}
public function open_connection(){
$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD);
if(!$this->connection){
die("Database connection failed: " . mysql_error());
} else {
$db_select = mysql_select_db(DB_NAME);
if(!$db_select){
die("Database selection failed: " . mysql_error());
}
}
}
}
$database = new MySQLDatabase();
?>
Here's the problem, whenever I include 'database.php' on another page,
theres an error stating that I have undefined constants. Of course the
database connection doesnt work either. Now, if I define these constants
inside of 'database.php' instead of doing it on a different file, this
works.
Is there a reason why this is happening?

No comments:

Post a Comment