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