$_SERVER['DOCUMENT_ROOT'] '/includes/db.inc.php';

Hi there,

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[‘DOCUMENT_ROOT’] . ‘/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[‘DOCUMENT_ROOT’] 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[‘DOCUMENT_ROOT’] won’t work? Do I need to specify something in a php config file or something?

Volterony

Add

var_dump($_SERVER['DOCUMENT_ROOT']);

Ensure the DOCUMENT_ROOT is indeed pointing to the location your includes folder is located in.

Hi cpradio,

Thanks for getting back to me.

Sorry are you suggesting that I add: var_dump($_SERVER[‘DOCUMENT_ROOT’]); 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[‘DOCUMENT_ROOT’]); in my controller file?

Thanks for you help.

Volterony

Make a test.php and then echo var_dump($_SERVER[‘DOCUMENT_ROOT’]) ; to see what the output says.

string(28) “/Library/WebServer/Documents”

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):

Users/MyName/Sites/MyPHP/sitepointphp/chapter7/includes

The path of my controller file:

Users/MyName/Sites/MyPHP/sitepointphp/chapter7/admin/index.html

In the controller file the include path to my database connection file: include $_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/db.inc.php’;

Is the Document_Root wrong and needs changed? I am running on 127.0.0.1 and not localhost.

Regards
Volterony

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

include 'C:/Users/MyName/Sites/MyPHP/sitepointphp/chapter7/includes/db.inc.php';

hi cpradio,

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.

Best regards
Volterony

You should read up on apache virtual hosts. It will allow you to have site specific directives, such as the document root.