douluchuo0801
douluchuo0801
2015-09-15 01:58
浏览 60
已采纳

使用未定义的常量和mysqli_connect():[重复]

This question already has an answer here:

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)

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dslh32311
    dslh32311 2015-09-15 02:16
    已采纳

    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

    点赞 评论

相关推荐