-1

I don't know why I'm getting this error. I'm just following what tutorial say. How do I fix this?

<?php

define ("HOST_NAME", "localhost");
define ("HOST_USER", "root");
define ("HOST_PASSWORD", "");
define ("DB_NAME", "db_user");
?>

And actual code

<?php 
require_once("config.php");

class MySQLDatabase{

private $connection;

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

public function open_connection(){
    $this->connection = mysqli_connect(HOST_NAME,HOST_USER,HOST_PASSWORD,DB_NAME);
    if(mysqli_connect_errno()){
      die("Database connection failed" .
           mysqli_connect_error() .   
             " ( " . mysqli_connect_errno . ")"
      );
    }

}

Errors

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

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

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

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

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

Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\first-project\includes\database.php on line 13

Notice: Use of undefined constant mysqli_connect_errno - assumed 'mysqli_connect_errno' in C:\xampp\htdocs\first-project\includes\database.php on line 17 Database connection failedphp_network_getaddresses: getaddrinfo failed: No such host is known. ( mysqli_connect_errno)

Community
  • 1
  • 1
shin
  • 3
  • 1
  • 4
  • does first code-block is `config.php` ? and where this file is located? – M0rtiis Sep 15 '15 at 02:03
  • depends on what your origin file is in accessing it. I don't see where you're using that class. – Funk Forty Niner Sep 15 '15 at 02:09
  • Do you have multiple config.php files in different directories? Maybe you're not including the one you think you are. – Mike Sep 15 '15 at 02:11
  • if you're not `require`ing the file where you are `define`ing those constants in `config.php`, you can't use them. Try moving the `define` statements into database.php and if that fixes the error, make sure you include the defines correctly. –  Sep 15 '15 at 02:14
  • @mike but i only have 1 config file – shin Sep 15 '15 at 02:16
  • Error reporting states `Parse error: syntax error, unexpected end of file, expecting function (T_FUNCTION)...` being caused by the missing brace as P. Leger stated in his answer. You accepted the wrong one, in my view. http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Sep 15 '15 at 02:25
  • @Fred -ii- really dont see somthing about syntax errors in author message... just not full code provided – M0rtiis Sep 15 '15 at 02:26
  • @M0rtiis Please don't argue; it's a parse error. Do you think I didn't test this? – Funk Forty Niner Sep 15 '15 at 02:27
  • @Fred nope.. we see all of errors he recieves. nothing about syntax. not all of his code he posted – M0rtiis Sep 15 '15 at 02:28
  • @M0rtiis open your code editor and put their class code in there, then come back and tell me different. Count the braces and match them. – Funk Forty Niner Sep 15 '15 at 02:29
  • i see it without copying his code to my editor. also i see Notices and Warnings he recieves. you too? i dont see there any syntax errors. you too? what does it mean? he forgot to copy last line of his code. Syntax error is certainly not a SUBJECT – M0rtiis Sep 15 '15 at 02:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89631/discussion-between-m0rtiis-and-fred-ii). – M0rtiis Sep 15 '15 at 03:00

1 Answers1

0

The easiest solution (far from best) will be to replace

require_once("config.php");

with

define ("HOST_NAME", "localhost");
define ("HOST_USER", "root");
define ("HOST_PASSWORD", "");
define ("DB_NAME", "db_user");

and forget about config.php

this will not break you app-logic in other places, i believe noone except MySQLDatabase class needs them

M0rtiis
  • 3,676
  • 1
  • 15
  • 22