Reputation: 653
I have a web site that I am working on, it is fairly simple but it uses a MySQL database. I did initial development of this site in Linux using Apache 2.2.25, PHP 5.4.17, and MySQL 5.1. Everything worked fine on this server stack. However, I am having an incredibly strange error on Windows, using the latest WAMP server. There are a number of files involved in this site, but as far as the index page goes, there is a basic layout of includes:
index.php
->header.php
--->connection.php
--->functions.php
->footer.php
When I load the index page, the header is definitely being included properly, as is the functions.php, which contains all the site and database functions, including the function to open a SQL connection using mysqli. However, the connection.php, which contains defines for the server information, does not seem to get read at all when the page is loaded. I get errors such as
"Notice: Use of undefined constant DB_HOST - assumed 'DB_HOST' in D:\wamp\www\functions.php on line 2",
even though these are definitely defined in connection.php, and connection.php is included first. I have also tried reorganizing the includes so they go like this:
index.php
->header.php
--->functions.php
----->connection.php
That also did not change the problem. I even tried adding a die() and a mail() into the connection.php for testing purposes. Nothing in that file is getting executed. I would like to repeat that this exact code and file layout worked just fine on the LAMP stack. I have no idea why this is happening, as there doesn't seem to be a reason for it.
Edit: This should have been in my original post. The code for my defines (in connection.php) is as follows:
define("DB_NAME", "lyricsdb");
define("DB_USER", "lyricsdb");
define("DB_PASS", "XXXXXXXXX");
define("DB_HOST", "localhost");
And these constants are referenced in the following function in functions.php:
function sql_connect($db_host = DB_HOST, $db_user = DB_USER, $db_pass = DB_PASS, $db_name = DB_NAME) {
$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name) or die('Could not connect to database.');
return $con;
}
Edit 2: It works fine if I move my defines into the top of the functions.php file. What is this I don't even?
Upvotes: 0
Views: 858
Reputation: 3437
It is probable that your file is being included, but you will always get warnings about undefined constant if you reference array keys like this
$db[DB_HOST];
They should be referenced as
$db['DB_HOST']
If you are sure your variable is defined, then check you have included the ''s around the variable name so it looks for a key name rather than looking for something that has been defined as a constant. This is just warning you that if there was a define('DB_HOST','something');
then that would take precedence over your $db['DB_HOST']
and it would instead look for $db['something']
Upvotes: 1