Page 1 of 1

Use of undefined constant - Xampp 1.8.1

PostPosted: 09. April 2013 09:50
by ianonyk
I'm using Xampp and I got this frustrating error and couldn't figure out how to fix it.
Code: Select all
Notice: Use of undefined constant DB_SERVER - assumed 'DB_SERVER' in C:\xampp-portable\htdocs\gallery\includes\database.php on line 12


Here is my index.php file located in "gallery/public/" folder
Code: Select all
<?php
require_once("../includes/database.php");

if (isset($database)){
echo "Is this working?";
}
?>


Here is the config.php located in "gallery/includes/" folder
Code: Select all
<?php
define('DB_SERVER', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'photo_gallery');
?>


and here is the database.php located in "gallery/includes/" folder
Code: Select all
<?php
require_once("config.php");
class MySQLDatabase {
   
   private $connection;
   
  function __construct() {
    $this->open_connection();
  }

   public function open_connection() {
      $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
      if (!$this->connection) {
         die("Database connection failed: " . mysql_error());
      } else {
         $db_select = mysql_select_db(DB_NAME, $this->connection);
         if (!$db_select) {
            die("Database selection failed: " . mysql_error());
         }
      }
   }

   public function close_connection() {
      if(isset($this->connection)) {
         mysql_close($this->connection);
         unset($this->connection);
      }
   }
}

$database = new MySQLDatabase();
$db =& $database;

?>

Re: Use of undefined constant - Xampp 1.8.1

PostPosted: 09. April 2013 10:35
by Altrea
Just a test:
please rename your file config.php to something very unique like config123.php and change the require statement too.
I guess it is conflicting with the config.php file inside of PHP PEAR.

Re: Use of undefined constant - Xampp 1.8.1

PostPosted: 09. April 2013 12:29
by ianonyk
That works. Thanks. But do you have any other solution that do not require renaming config.php?
Something with BASE magic varible?

Re: Use of undefined constant - Xampp 1.8.1

PostPosted: 09. April 2013 15:40
by Altrea
ianonyk wrote:That works. Thanks. But do you have any other solution that do not require renaming config.php?
Something with BASE magic varible?

Two more possible solutions:
  • require your file with
    Code: Select all
    require dirname(__FILE__) . '/config.php';
  • remove PEAR from the php.ini include_path setting, if you will never need it

best wishes,
Altrea