Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts

    Database connection not working under root

    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.
    Last edited by Chris Hick; 01-28-2012 at 05:12 AM.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #2
    New Coder
    Join Date
    Jan 2012
    Location
    Scarborough,United Kingdom
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you post some code please.

  • #3
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    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.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    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.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by jmj001 View Post
    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).

  • #7
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    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

  • #8
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    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.
    PHP Code:
    public function open_connection() {
            
    $this->connection mysqli_connect(DB_HOSTDB_USERDB_PASSWORDDB_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.
    Last edited by Chris Hick; 01-27-2012 at 07:02 AM.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Die with the error using mysqli_connect_error(). What does it say the problem is?

  • #10
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    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
    Last edited by Chris Hick; 01-28-2012 at 02:16 AM.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #11
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    do you have a constant set defining DB_HOST?

    I would guess that the constant is not being processed

  • #12
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    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
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    PHP Code:
    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.

  • #14
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    I have a initialize file that looks like this:
    PHP Code:
    <?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.
    PHP Code:
    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)
    Last edited by Chris Hick; 01-28-2012 at 04:46 AM.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #15
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    PHP Code:
    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...


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •