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.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    74
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question Issues using a db_config.inc.php file? [PHP5]

    I was reading up on how to setup a PHP5 class for Database Interface and a few places mentioned using a config.inc.php file to store the database information ... So this is what I did:

    Database.class.php
    Code:
    require_once 'db_config.inc.php';
    
    class Database
    {
        // Global Variables
        private $server   = "";         //database server
        private $user     = "";         //database login name
        private $password = "";         //database login password
        private $database = "";         //database name
    
        function Database()
        {
            $this->server   = $db_config['server'];
        	$this->user     = $db_config['user'];
        	$this->password = $db_config['password'];
        	$this->database = $db_config['database'];

    db_config.inc.php
    Code:
    <?php
        //database server
        $db_config['server'] = "localhost";
    
        //database login name
        $db_config['user'] = "username";
    
        //database login password
        $db_config['password'] = "password";
    
        //database name
        $db_config['database'] = "username_mydata";
    ?>
    I would have assumed this would work fine - but oddly enough it generates the following error message when I try to run it:
    PHP Notice: Undefined variable: db_config in Database.class.php on line 30
    PHP Notice: Undefined variable: db_config in Database.class.php on line 31
    ... and on and on for all of them ...

    What am I doing wrong? Is this not the correct/standard approach?
    Any clues, hints, ideas would be much appreciated.

    Thanks,

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    $db_config is obviously out of scope.

  • #3
    New Coder
    Join Date
    Jan 2005
    Posts
    74
    Thanks
    0
    Thanked 1 Time in 1 Post
    I seem what you mean - from the errors that makes sense...
    But why and how can I solve it?

    Thanks,

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Thanked 4 Times in 4 Posts
    I am pretty new to oop but it seems like you need to include all those $db variables as arguments when you instatiate your object and edit your class to take those arguments.

  • #5
    New Coder
    Join Date
    Aug 2004
    Posts
    24
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Shaitan00 View Post
    What am I doing wrong? Is this not the correct/standard approach?
    Any clues, hints, ideas would be much appreciated.

    Thanks,
    Hey,

    An easy way to have this set up, would be similar to what i use.
    Code:
    /* config.inc.php */
    $dbArray = array('dbServer' => 'servername', 'dbUser' => 'username', 'dbPass' => 'password', 'dbName' => 'databasename');
    Then to access this array of server variables, your database class would be similar to:
    Code:
    /* database.class.php */
    
    require_once('config.inc.php');
    
    class Database
    {
        // Global Variables
        private $server;         //database server
        private $user;         //database login name
        private $password;         //database login password
        private $database;         //database name
    
        function Database()
        {
            global $dbArray;
            $this->server = $dbArray['dbServer'];
        	$this->user = $dbArray['dbUser'];
        	$this->password = $dbArray['dbPass'];
        	$this->database = $dbArray['dbName'];
    The problem you were facing mainly was the array which you were storing your database
    variables in were not accessible inside the class because it did not exist.

    All that is needed, is to have a global declaration inside your constructor to alert that
    function that it needs to look outside the class for the array, rather than inside it.

    Also in my config, i have shortened the defining of your database variables through 'key => value' pairs.

    Simply a
    Code:
    dbArray('dbServer' => 'servername');
    Where dbArray['dbServer'] is the key name, but will return the value of 'servername' when called/

    Hope this helped


    EDIT: sorry, did not notice the age on the thread
    Last edited by AnniHilatE; 04-22-2009 at 02:17 PM. Reason: Age of thread


  •  

    Posting Permissions

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