Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP define() doesn't seem to be working with include()

Tags:

include

php

class

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.

like image 997
Saladin Akara Avatar asked Aug 18 '10 10:08

Saladin Akara


1 Answers

working

<?php
define('KLINGON_SEPARATOR', '');
?>

not working

<?php
define('KLINGON_SEPARATOR', '');

silly... IDEA says "redundant closing tag"

like image 150
Martin Pfeffer Avatar answered Sep 19 '22 18:09

Martin Pfeffer