0

config.php

<?
// Database Constants 
define("DB_SERVER", "localhost");
define("DB_USER", "gallery");
define("DB_PASS", "phpOTL123");
define("DB_DATABASE", "photo_gallery");
?>

database.php

<?php
require_once('config.php'); 
class  MySQLDatabase
{
private $connection;
        public function open_connection() {
        $this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS,DB_NAME);
        if (!$this->connection) {
            die("Database connection failed: " . mysqli_error());
        }
        }
    }
$database=new MySQLDatabase();
$database->open_connection();  
?>

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

Warning: Use of undefined constant DB_SERVER - assumed 'DB_SERVER' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\photo_gallery\includes\database.php on line 6

Warning: Use of undefined constant DB_USER - assumed 'DB_USER' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\photo_gallery\includes\database.php on line 6

Warning: Use of undefined constant DB_PASS - assumed 'DB_PASS' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\photo_gallery\includes\database.php on line 6

Warning: Use of undefined constant DB_NAME - assumed 'DB_NAME' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\photo_gallery\includes\database.php on line 6

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

Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\photo_gallery\includes\database.php on line 6 Database connection failed: mysqli_error()

Zeyad Sharo
  • 11
  • 1
  • 5
  • 5
    Try changing that `` in `config.php` to a ` – ceejayoz Mar 11 '19 at 18:25
  • 3
    You can also get rid of the trailing `?>`. They don't have any purpose here and can only lead to subtle bugs. – Álvaro González Mar 11 '19 at 18:29
  • Also check that you are setting `DB_PASSWORD` and using `DB_PASS` – AleOtero93 Mar 11 '19 at 18:37
  • 1
    : "It looks like you're writing your own ORM. Have you considered using one that's already written, tested, and widely supported like [RedBeanPHP](https://redbeanphp.com/), [Doctrine](http://www.doctrine-project.org/), [Propel](http://propelorm.org/) or [Eloquent](https://laravel.com/docs/master/eloquent)?" – tadman Mar 11 '19 at 18:48

1 Answers1

2

PHP short tags are recommended against, because they're usually off.

Thus, your <? isn't doing anything, and your database credentials are being treated as plain text instead of PHP. Changing it to the full <?php fixes it.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368