常量问题和连接数据库PHP

I'm getting these errors in my on screen

Notice: Use of undefined constant DB_SERVER - assumed 'DB_SERVER' in C:\xampp\htdocs\photo_gallery\includes\database.php on line 13

Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in C:\xampp\htdocs\photo_gallery\includes\database.php on line 13

Notice: Use of undefined constant DB_PASS - assumed 'DB_PASS' in C:\xampp\htdocs\photo_gallery\includes\database.php on line 13

Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\photo_gallery\includes\database.php on line 13

Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\photo_gallery\includes\database.php on line 13 Database connection failed: php_network_getaddresses: getaddrinfo failed: No such host is known.

Here is my code:

<?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());
            }
        }
    }

My constants are defined as so:

<?php

// Database Constants
defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER')   ? null : define("DB_USER", "gallery");
defined('DB_PASS')   ? null : define("DB_PASS", "phpOTL123");
defined('DB_NAME')   ? null : define("DB_NAME", "photo_gallery");

?>

What am I doing wrong?

it seems that constants are not getting available to the mysql_connection function. Include the script in which constants are defined or define the constants in class MySQLDatabase itself.

I got the same errors and a small edit fixed it. By the way you should migrate to mysqli instead of mysql.

Here is my question : PHP constant not defined

You should replace the require_once in the database.php with the following code

require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . "config.php");

It will set the path to absolute and will work smoothly and fine