0

I am trying to create a simple photo gallery on my localhost using XAMPP.

On my includes folder I have three files:

-config.php (constants of database info)
-database.php (Which contains my database classes)
-functions.php (Which contains all my functions)

Here's my CONFIG file:

    <?php

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

?>

Here's my DATABASE file:

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

    public function query($sql) {
        $result = mysql_query($sql, $this->connection);
        $this->confirm_query($result);
        return $result;
    }

    public function mysql_prep( $value ) {
        $magic_quotes_active = get_magic_quotes_gpc();
        $new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
        if( $new_enough_php ) { // PHP v4.3.0 or higher
            // undo any magic quote effects so mysql_real_escape_string can do the work
            if( $magic_quotes_active ) { $value = stripslashes( $value ); }
            $value = mysql_real_escape_string( $value );
        } else { // before PHP v4.3.0
            // if magic quotes aren't already on then add slashes manually
            if( !$magic_quotes_active ) { $value = addslashes( $value ); }
            // if magic quotes are active, then the slashes already exist
        }
        return $value;
    }

    private function confirm_query($result) {
        if (!$result) {
            die("Database query failed: " . mysql_error());
        }
    }

}

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

?>

When I tried to test if my database is working using:

    <?php
require_once("../includes/database.php");


if(isset($database)){
    echo "Database is working?";
}else{
    echo "Database not working!";
}



?>

on my index.php file which is on a different folder for public access, I got the following errors:

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.

I tried rechecking my database information but I am confident that I got them correctly. Any idea what's causing this? Thanks in advance for those who can help.

1

2 Answers 2

1

Do not trust relative path when including files. Always use __DIR__ when you want to refer current directory.

require_once(__DIR__ . '/path/to/you/file.php');

Why?

According to PHP Documentation

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing. The include construct will emit a warning if it cannot find a file; this is different behavior from require, which will emit a fatal error.

If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.

If you don't define a path e.g. require_once("config.php");, PHP will get the file from include_path list.

Here is a XMAPP default include path

.;C:\xampp\php\PEAR

Unfortunately, PEAR (pear) directory have a file Config.php, thus PHP include this file instead of fallback this path and read your config.php.

Sign up to request clarification or add additional context in comments.

Comments

0

checking defined or not is done by defined() but not by comparing it to null defined('DBSERVER') or define('DBSERVER', 'localhost') should help

1 Comment

I tried to remove the comparation part of the code however still getting the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.