PHP常量未定义 [英] PHP constant not defined
问题描述
我正在使用一个数据库项目,我创建了一个单独的文件,以保持所有的数据库信息名称'config.php'。对于数据库,我将另一个'database.php'文件保存在同一目录中。
I am working with a database project and I have created a seperate file to keep all the database information name 'config.php'. And for the database I keep another 'database.php' file in the same directory.
----------------- ------- config.php --------------
------------------------config.php--------------
<?php
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',"photomania");
?>
--------------------- --- databae.php -------------
------------------------databae.php-------------
<?php
require_once("config.php");
class MySQLDatabase{
private $conn;
function __construct(){
$this->open_connection();
}
public function open_connection(){
$this->conn = mysqli_connect(DB_SERVER,DB_USER,DB_PASS);
if(!$this->conn){
die("Database Connection Failure :(".mysql_errno());
}else{
$db_select = mysqli_select_db($this->conn,DB_NAME);
if(!$db_select){
die("Can't select the database".mysql_errno());
}
}
}
public function close_connection(){
if(isset($this->conn)){
mysqli_close($this->conn);
unset($this->conn);
}
}
}
$database = new MySQLDatabase;
?>
问题是当我将database.php包含在索引文件中时使用
require_once("../includes/database.php");
it gives following errors.
注意:使用未定义的常量DB_SERVER - 在
中假定为DB_SERVERC:\XAMPP\htdocs\gallery\includes\database.php在第11行上
Notice: Use of undefined constant DB_SERVER - assumed 'DB_SERVER' in C:\XAMPP\htdocs\gallery\includes\database.php on line 11
注意:使用未定义的常量DB_USER - 在
中假定为DB_USERC:\XAMPP\htdocs\gallery\includes\database.php在第11行
Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in C:\XAMPP\htdocs\gallery\includes\database.php on line 11
注意:使用未定义的常量DB_PASS - 在
中假定为DB_PASSC:\XAMPP\htdocs\gallery\includes\database.php on line 11
Notice: Use of undefined constant DB_PASS - assumed 'DB_PASS' in C:\XAMPP\htdocs\gallery\includes\database.php on line 11
警告:mysqli_connect():php_network_getaddresses:getaddrinfo
失败:未知此类主机。 in
C:\XAMPP\htdocs\gallery\includes\database.php on line 11
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\XAMPP\htdocs\gallery\includes\database.php on line 11
警告:mysqli_connect():(HY000 / 2002):php_network_getaddresses:
getaddrinfo failed:没有这样的主机是已知的。 in
C:\XAMPP\htdocs\gallery\includes\database.php on line 11数据库
连接失败:(
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\XAMPP\htdocs\gallery\includes\database.php on line 11 Database Connection Failure :(
任何帮助
Any help ??????
推荐答案
您的问题似乎在下列行中
you problem seems to lie in following line
require_once("config.php");
尝试替换为
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . "config.php");
这篇关于PHP常量未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!