Page 1 of 1

require_once() giving headache

Posted: Sat May 30, 2009 4:36 pm
by twistedr
hi,

I am having a lot of trouble including a file in my project.

A low rundown: I have a directory _includes in my root directory. in this I have a database.php and a config.php. The database.php uses require_once() to include config.php. Now in the root directory i have a login.php which uses require.php to include the database.php.

The problem: on its own the database.php works fine. If i open login.php it doesnot seem to be including the config.php. Although it gives no error regarding config.php. But it doesnot recognise the varibles i defined in hte Config.php



The Code:

Config.php:

Code: Select all

<?php
define(DB_SERVER,"localhost");
define(DB_USER,"asdas");
define(DB_PASS,"asdas");
define(DB_NAME,"rasd");
?>
database.php

Code: Select all

<?php
    require_once("config.php");
    class MySQLDB{
        private $connection;
        
        //Basic Database Connectivity Methods
        function __construct(){
            $this->openConnection();
        }
        public function openConnection(){
            $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
            if(!$this->connection){
                die("Unable To Connect to dbServer: " . mysql_errno()); 
            }
            else{
                $db = mysql_select_db(DB_NAME, $this->connection);
                if(!$db){
                    die("Unable To Select Database: " . mysql_error());
                }
            }
        }
        public function closeConnection(){
            if(isset($this->connnection)){
                mysql_close($this->connection);
                unset($this->connection);
            }
        }
        
        //Quering Methods
        public function query($sql){
            $result = @mysql_query($sql);
            $this->confirmQuery();
            return $result;
        }
        
        private function confirmQuery($r){
            if(!$r){
                die("Query Failed: " . mysql_error());
            }
        }
        private function mysqlPrep($data){
            if(function_exists("mysql_real_escape_string")){
                if(get_magic_quotes_gpc()){ $data = stripslashes($data); }
                $data = mysql_real_escape_string($data);
            }
            else{
                if(!get_magic_quotes_gpc()){ $data = addslashes($data); }
            }
            return $data;
        }
    }
    
    $database = new MySQLDB;
    $database->openConnection;
?>

in login.php all i am doing is incuding this database.php.
But it always returns an mysql error : Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'DB_SERVER' (11001) in C:\xampp\htdocs\CMS\_includes\database.php on line 12
Unable To Connect to dbServer: 2005

if i open only database.php it works fine.

Please help. Just started with php a while ago!

Thanks

Re: require_once() giving headache

Posted: Sat May 30, 2009 4:52 pm
by Darhazer
Use the full path to the files:

Code: Select all

require_once( dirname( __FILE__) . '/config.php' )
Otherwise it tries to open the file from the current directory, which, obviously is different, depending of the directory in which the script is running

Re: require_once() giving headache

Posted: Sun May 31, 2009 3:09 am
by twistedr
Thanks it worked.

But I was copying this from Lynda.com php beyond the basics tutorial. The Tutor in it did not use the full path and it worked fine for him?

Re: require_once() giving headache

Posted: Sun May 31, 2009 6:53 am
by Darhazer
It depends on server configuration (include_path for example)
Using full path you are making it independent from the configuration