1
votes

I have a strange problem. I have one file index.php in the root, and 2 files include directory inside the root.

root > index.php , include
include > config.php , db.php

In include/db.php I defined MySQL connection parameters like this:

$hostname = 'localhost';
$username = 'root';
$password = '';
$dbname   = 'mydb';

Then I have included this file inside include/config.php :

include 'db.php';

And finally, I've included include/config.php inside index.php.
The problem I have is I have not access to database variables when I run index.php. They are only available if I run include/config.php. But I expected I must have all defined variables inside these 2 files when I included them in index.php.

What is the problem ?

1
index.php includes config.php includes db.php — Is that correct? Also, how do you know you don't have access to the variables? What does print_r(get_defined_vars()); output? - Amal Murali
Is it really the include() that does not work? You should get an error if that is the case! - arkascha
How have you tried to include config.php inside index.php? Syntax? - Kanishk Dudeja
@MohammadSaberi add this at the top of index.php and then show us the error error_reporting(-1); - zzlalani
I just tested locally and it works fine. I can only assume you're making a spelling mistake somewhere. - asprin

1 Answers

0
votes

if you have included config.php in index.php and db.php in config.php. Then index.php should be: include 'include/config.php'; and config.php should be: include 'include/db.php';