I am working through the Build Database Driven Website with PHP and Mysql by Kevin Yank edition 4.
I am having a problem connecting to my ijdb database when I use: include $SERVER['DOCUMENTROOT'] . '/includes/db.inc.php'; I am able to connect to the database when I don't use the include but have the connection code in my controller file. So the issue is with the $SERVER['DOCUMENTROOT'] which doesn't seem to work?
In the actual db.inc.php, I have ensured the connection details are correct: $link = mysqli_connect('127.0.0.1', '*****', '*******');
So is there any reasons why include $SERVER['DOCUMENTROOT'] won't work? Do I need to specify something in a php config file or something?
Ensure the DOCUMENT_ROOT is indeed pointing to the location your includes folder is located in.
Thanks for getting back to me.
Sorry are you suggesting that I add: var_dump($SERVER['DOCUMENTROOT']); to my php config file? Or do I need to add this in each file has an include ?
Or do I add the line: var_dump($SERVER['DOCUMENTROOT']); in my controller file?
Thanks for you help.
Make a test.php and then echo var_dump($SERVER['DOCUMENTROOT']) ; to see what the output says.
Okay, and what is the file path to your includes folder? Is it /Library/WebServer/Documents/includes?
The project path and the include folder (using a Mac):
The path of my controller file:
In the controller file the include path to my database connection file: include $SERVER['DOCUMENTROOT'] . '/includes/db.inc.php';
Is the Document_Root wrong and needs changed? I am running on 127.0.0.1 and not localhost.
Yes, you will need to edit your apache configuration to point to the directory that contains your includes folder.
OR, you can change the include to be an absolute path to your /includes/db.inc.php file, like so
Yes the absolute path works fine, and should do for now as I am only working through a book project. However I think I would change the DOCUMENT_ROOT if I were publishing this on a real server!
Really appreciate the time taken to help.
You should read up on apache virtual hosts. It will allow you to have site specific directives, such as the document root.
This topic is now closed. New replies are no longer allowed.