I have some database information stored in config.inc.php and i’m trying to use it to access the database in a class I have, but for some reason, the variables are null. heres the code:
<?php
require_once 'dbinterface.php';
require_once 'config.inc.php';
class user {
...
function user($id) {
$this->db = new db($DB['host'], $DB['user'], $DB['pass'], $DB['database']);
...
}
...
?>
and here’s config.inc.php:
<?php
$DB['host'] = '192.168.1.107';
$DB['user'] = '****';
$DB['pass'] = '****';
$DB['database'] = 'qa';
?>
I’m not sure why i’m getting nulls, but here’s my proof:
Fatal error: Uncaught exception
‘Exception’ with message ‘No database
selected’ in
D:\development\PHP\qanda\dbinterface.php:18
Stack trace: #0
D:\development\PHP\qanda\user.class.php(17):
db->db(NULL, NULL, NULL, NULL) #1
D:\development\PHP\qanda\log.php(17):
user->user(‘1’) #2 {main} thrown in
D:\development\PHP\qanda\dbinterface.php
on line 18
You would have to declare
$DBasglobalfor this to work:And in your class definition: