1

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
  • Is that all that's in config.inc.php? Commented Jul 21, 2009 at 20:15

5 Answers 5

4

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']);
        ...
    }
Sign up to request clarification or add additional context in comments.

Comments

3

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']);
    ...
}

Comments

1

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;
    }
}

3 Comments

+1 - either move the require or add "global $DB" at the start of the function
Also if you develop with NOTICES turned on you'd catch this kind of stuff much more easliy
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.
1

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);
    ...
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.