Ahoura Ghotbi
Ahoura Ghotbi

Reputation: 2896

Use of undefined constant DB_SERVER - assumed 'DB_SERVER'

I have the following php code :

$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "photos";

define(DB_SERVER,$db_host);
define(DB_USER,$db_user);
define(DB_PASS,$db_pass);
define(DB_NAME,$db_name);

I am defining DB_SERVER and all the other ones as you can see above, but for some reason I get the following errors :

Notice: Use of undefined constant DB_SERVER - assumed 'DB_SERVER' in /Users/ahoura/Sites/photos/include/config.php on line 34 Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in /Users/ahoura/Sites/photos/include/config.php on line 35 Notice: Use of undefined constant DB_PASS - assumed 'DB_PASS' in /Users/ahoura/Sites/photos/include/config.php on line 36 Notice: Use of undefined constant DB_NAME - assumed 'DB_NAME' in /Users/ahoura/Sites/photos/include/config.php on line 37

What am I doing wrong!?!?!

Upvotes: 2

Views: 22171

Answers (4)

Chukwudi Nwobodo
Chukwudi Nwobodo

Reputation: 1

This does not answer the question but it is just for people who might have this issue, when declaring variables for global constants please use single quotes and not double quotes. I made this mistake and it took me a week to figure the issue out once it was uploaded to the server.

Do not do this:

define("DB_SERVER",$db_host);
define("DB_USER",$db_user);
define("DB_PASS",$db_pass);
define("DB_NAME",$db_name);

Do this:

define('DB_SERVER',$db_host);
define('DB_USER',$db_user);
define('DB_PASS',$db_pass);
define('DB_NAME',$db_name);

Upvotes: 0

Khayelihle Tshuma
Khayelihle Tshuma

Reputation: 31

Encountered the same issue, on research these worked for me :
1. Rename your file config.php to something very unique like config123.php, be ure to change the require statement too. Probably caused by a conflict with the config.php file inside of PHP PEAR.
2. Require your file with:
require dirname(FILE) . '/config.php';
3. Remove PEAR from the php.ini include_path setting, if you will never need it.

Source :https://community.apachefriends.org/f/viewtopic.php?p=204776

Upvotes: -1

Dharmesh Patel
Dharmesh Patel

Reputation: 1

I will just use database info in to class directly rather then importing from different files. This could be the problem with XAMPP 1.6.7 the latest version I guess.anyways you can try this out.

<?php

    class MySQLDatabase {

        private $connection;

        function __construct() {
           $this->open_connection();
        }

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

    $database = new MySQLDatabase();
?>

Upvotes: -2

Michael Berkowski
Michael Berkowski

Reputation: 270617

You need to quote the names of your new constants in define()

define('DB_SERVER',$db_host);
define('DB_USER',$db_user);
define('DB_PASS',$db_pass);
define('DB_NAME',$db_name);

The notice indicates that PHP did in fact treat them as quoted strings because it could not locate existing constants with those names, but this is bad practice to rely on. define() takes a string as the name of the constant to be defined, and its intended value.

Upvotes: 13

Related Questions