...

View Full Version : Issues using a db_config.inc.php file? [PHP5]



Shaitan00
03-26-2009, 05:35 AM
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


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


<?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,

kbluhm
03-26-2009, 07:18 AM
$db_config is obviously out of scope.

Shaitan00
03-26-2009, 08:01 AM
I seem what you mean - from the errors that makes sense...
But why and how can I solve it?

Thanks,

F-b0mb
03-26-2009, 11:11 AM
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.

AnniHilatE
04-22-2009, 01:16 PM
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.


/* 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:


/* 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
dbArray('dbServer' => 'servername');
Where dbArray['dbServer'] is the key name, but will return the value of 'servername' when called/

Hope this helped :D


EDIT: sorry, did not notice the age on the thread



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum