...

View Full Version : Resolved Database connection not working under root



Chris Hick
01-26-2012, 10:46 PM
Ok, I posted something like this a while back. I have my classes and the config files in a folder inside a folder above the public_html folder(root). Now, below my root folder, I have a file called login.php. When I go the file on my browser it tells me unable to connect to database. Everything is right as far as paths and the configuration. I don't understand what is going on.

thewebguy2011
01-26-2012, 10:47 PM
Can you post some code please.

Chris Hick
01-26-2012, 10:56 PM
I'm uncertain as to what code you are speaking about. Because my classes all work if I put them in a folder under root, but that is not what I wanna do. I want to restrict access to these files so I placed them in a folder inside a folder above root. I updated all the paths and then my class errors tells me that it is unable to connect to database.

Fou-Lu
01-26-2012, 11:18 PM
I believe root by default is privileged as root@localhost. I don't think it has default privileges of root@%.
So this will only work if connection is occurring from localhost.

jmj001
01-27-2012, 02:03 AM
I think he means root as in the web root folder, not the username root... though I may be mistaken.

It will be a filepath issue where the script in the folder you are accessing is not getting access to the db class to make a connection.

Paste some errors here it may help troubleshooting.

Fou-Lu
01-27-2012, 02:20 AM
I think he means root as in the web root folder, not the username root... though I may be mistaken.

It will be a filepath issue where the script in the folder you are accessing is not getting access to the db class to make a connection.

Paste some errors here it may help troubleshooting.

*facepalm*
You're absolutely right!

Back to the error then. Change whatever error control you have on your connection to trigger the error (mysql_error() if you are using the mysql library) and post that.
You cannot just change the location of your files and expect them to work without modifying scripts including them. To do this, your option is to rely solely on the include_path of PHP, so when you alter a location you can add to this path via ini_set. Then it will seek files in an alternate location.
If you are using OOP, you can chain a classloader to the __autoload function so you can use a namespace. This is particularly handy since with namespace usage you must issue a USE command, and this automatically loads the class as well (so I can do use \Myroot\MyClass; for example).

jmj001
01-27-2012, 02:38 AM
it's highly likely that since you have moved these files above the web root that the server may have basedir restriction not allowing scripts access to them.

if they are files that consist entirely of php classes and have no screen output unless the class or function is specifically called by other scripts there's not much point in moving them out of the web root levels...

if it's an issue that when you go to www. domain. com/folder/ and your server shows a list of the files, then just put an index.html file in the folder which will then return a blank screen .. or turn off the Indexes option at the server level if you have access to that

Chris Hick
01-27-2012, 02:59 AM
Fou, I make use of the __autoload function. I know my classes are being accessed. Specifically, my database class. Because the error being printed to the screen is the custom error for that class.


public function open_connection() {
$this->connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if(!$this->connection) {
die("Database connection failed. ");
}
}



The words Database Connection failed is what is appearing on the screen.

EDIT: I think maybe it has something to do with the htaccess file.

Fou-Lu
01-27-2012, 01:17 PM
Die with the error using mysqli_connect_error(). What does it say the problem is?

Chris Hick
01-27-2012, 09:59 PM
It returned this error. Unknown MySQL server host 'DB_HOST' (1)


I know I have the database host right bc its the same as when I place it under root in another folder

jmj001
01-28-2012, 02:48 AM
do you have a constant set defining DB_HOST?

I would guess that the constant is not being processed

Chris Hick
01-28-2012, 02:59 AM
yeah, in my config file all my database connection information are in constants. I don't understand how it processes in a folder under root but not when I place it above root and a file under root

Fou-Lu
01-28-2012, 03:10 AM
The config file is not included. DB_HOST is literal since PHP's default fallback is always to treat as a string, so it literally thinks you are using DB_HOST.

Location is quite irrelevant so long as you can read it. Make sure you enable your error reporting:


ini_set('display_errors', 1);
error_reporting(E_ALL);

If it can't find the file it will issue a warning on an include / fatal error on require. If it cannot open the file it should indicate that it is not readable.
Also, since this is a method I should mention this as well. DB_HOST et al have to be declared as define('DB_HOST', ...); and not via const. Chances are you have done this with define, but just a heads up that a const is linked into its scope such as a class or namespace level.

Chris Hick
01-28-2012, 03:37 AM
I have a initialize file that looks like this:


<?php
// load config file first
require_once('config.php');

// load basic functions next so that everything after can use them
require_once('functions.php');

// load core objects
require_once('session.php');
require_once('database.php');
require_once('database_object.php');
require_once('pagination.php');
require_once('getter.php');
require_once("forvalidation.php");

// load database-related classes
require_once('user.php');
require_once('administration.php');

?>



So, I know its being included. Then, in my file that is under root, I have a line that gets this file.


require_once('../database_info/includes/initialize.php');


Which goes back one directory into database_info into includes where it accesses the initialize file. So, I really don't see how its not accessing it.


Ok, so I've put error reporting in the file and it shows this.
Notice: Use of undefined constant DB_HOST - assumed 'DB_HOST' in /home7/rodgameo/database_info/includes/database.php on line 19 Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in /home7/rodgameo/database_info/includes/database.php on line 19 Notice: Use of undefined constant DB_PASSWORD - assumed 'DB_PASSWORD' in /home7/rodgameo/database_info/includes/database.php on line 19 Notice: Use of undefined constant DB_NAME - assumed 'DB_NAME' in /home7/rodgameo/database_info/includes/database.php on line 19 Warning: mysqli_connect(): (HY000/2005): Unknown MySQL server host 'DB_HOST' (1) in /home7/rodgameo/database_info/includes/database.php on line 19 Database connection failed. Unknown MySQL server host 'DB_HOST' (1)

jmj001
01-28-2012, 03:44 AM
echo DB_HOST;

when that show's you the actual hostname then you should be right to go

the webserver prob won't have access the levels below root

perhaps specify the full path in the require() like: /var/www/html/xxxx/whatever...

Chris Hick
01-28-2012, 04:11 AM
Whooo, I got it to work. for some reason. Having its name as config.php would not let it read. but I changed the name and bam it worked.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum