views:

283

answers:

5

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

+1  A: 

You're including the files outside of the class's scope. If you move the requires to inside the function, it will work as expected.

A better option would be to include it inside a constructor, loop through the $DB array, and assign its values to $this->DB, so you can access it in any function of your class. You'll also need to modify your functions to use $this->DB instead of $DB.

function __construct() {
    require_once 'dbinterface.php';
    $this->DB = array();
    foreach ($DB as $key => $value) {
        $this->DB[$key] = $value;
    }
}
jimyi
+1 - either move the require or add "global $DB" at the start of the function
Greg
Also if you develop with NOTICES turned on you'd catch this kind of stuff much more easliy
Greg
require, and especially require_once are very intensive on the cpu, so if this function gets called multiple times in a given script, you tend to waste resources.
davethegr8
+3  A: 

You have a variable scoping issue. If your config.inc file is included in the global context, then this should work:

function user($id) {
    global $DB;
    $this->db = new db($DB['host'], $DB['user'], $DB['pass'], $DB['database']);
    ...
}
zombat
+4  A: 

You would have to declare $DB as global for this to work:

global $DB;

$DB['host'] = '192.168.1.107';
$DB['user'] = '****';
$DB['pass'] = '****';
$DB['database'] = 'qa';

And in your class definition:

function user($id) {
        global $DB;
        $this->db = new db($DB['host'], $DB['user'], $DB['pass'], $DB['database']);
        ...
    }
John Rasch
A: 

Check out the PHP help page on variable scope and in particular the "globals" keyword. It should get you what you need. Good luck!

Mike
+1  A: 

Generally, using global variables is a bad idea. In this, case it works but it isn't optimal. The best idea is to use definitions.

define('DB_HOST', '192.168.1.107');
define('DB_USER', '****');
define('DB_PASS', '****');
define('DB_DATABASE', 'qa');

...

function user($id) {
    $this->db = new db(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
    ...
}
davethegr8
+1 for use of constants.
Residuum