views:

45

answers:

1

I've been trying my hand at OO PHP, and currently have three files. I have a class_lib.php which, at the moment, just has a databaseServer class, an index.php file and a definitions.php file. I want to put all my sensitive database info into the definitions file. However, when I do, I get an error when trying to connect to the database: "Unkown server DB_HOST". My definitions file is:

<?php
define("DB_HOST","localhost");
define("DB_USER","root");
define("DB_PASS","password");
define("DB_NAME","database");
?>

Then I use them in the index file like so:

include('definitions.php');
include('class_lib.php');

$testing = new databaseServer();

$testing->connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);

And the function I use in the databaseServer class is this:

    function connect($host,$user,$pw,$db) {
        $this->con = mysql_connect($host,$user,$pw);
        if (!$this->con) {
            die('Could not connect: ' . mysql_error());
            }
        $this->selectDb($db);
        }

    function selectDb($database) {
        $this->db = mysql_select_db($database,$this->con);
        if (!$this->db) {
            echo "Could not Select database: " . mysql_error();
            }
        }

Any ideas why this would not work? I've also tried putting the definitions file into an include in the class_lib file, but it still doesn't work.

A: 

This should work fine, and I've never seen it not work.

  • Make 100% sure the includes are in the correct order (the defines need to be loaded first)

  • Make test outputs of the constant values in the "definitions.php" file and the index file

  • Make 100% sure you are calling the right files

Pekka