奇怪的php需要/包含错误

时间:2014-03-13 10:42:18

标签: php mysql

我是一个相对较新的开发人员但知道一些东西。

我正在做一个关于PHP的教程除了Lynda的基础知识,并且正在尝试创建数据库类。

在课堂上写完所有内容后,我试图查看是否已建立连接。

require_once("../includes/database.php");
if(isset($database)) {
    echo "true<br />";
}else {
    echo "false<br />";
}

以上代码是我的测试,看它是否正常

问题是我试图使用“require_once(”config.php“);”在数据库类中。

require_once("config.php");//line 13

class MySQLDatabase{
   //code that is not important for this issue
}

这给了我页面上的以下错误:

 Notice: Use of undefined constant DB_SERVER - assumed 'DB_SERVER' in       E:\ProgramFiles\xampp\htdocs\photo_gallery\includes\database.php on line 13

 Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in E:\ProgramFiles\xampp\htdocs\photo_gallery\includes\database.php on line 13

 Notice: Use of undefined constant DB_PASS - assumed 'DB_PASS' in      E:\ProgramFiles\xampp\htdocs\photo_gallery\includes\database.php on line 13
 Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in E:\ProgramFiles\xampp\htdocs\photo_gallery\includes\database.php on line 13

 Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in E:\ProgramFiles\xampp\htdocs\photo_gallery\includes\database.php on line 13

 Warning: mysql_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in E:\ProgramFiles\xampp\htdocs\photo_gallery\includes\database.php on line 13

数据库连接失败:php_network_getaddresses:getaddrinfo失败:没有这样的主机

如果我使用(下面的代码)而不是require / require_once / include / include_once部分它可以工作!

define("DB_SERVER", "localhost");
define("DB_USER", "gallery");
define("DB_PASS", "123465");
define("DB_NAME", "photo_gallery");

这不是我第一次使用require / include ...但是我真的很困惑为什么这不能用这种方法: - / (一旦我完成了DB课程,我会将所有内容更改为mysqli,因为它是教程。不,我无法访问练习文件)

(对不起,长篇文章)

2 个答案:

答案 0 :(得分:0)

听起来像在你的config.php中,你在定义常量时错过了引号。

wrong:
define(DB_SERVER, "localhost");
right:
define("DB_SERVER", "localhost");

仔细检查,或发布config.php中的相关部分:)

答案 1 :(得分:0)

尝试按以下方式定义常量。

defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER')   ? null : define("DB_USER", "gallery");
defined('DB_PASS')   ? null : define("DB_PASS", "123456");
defined('DB_NAME')   ? null : define("DB_NAME", "photo_gallery");

删除Notice: Use of undefined constant DB_SERVER通知。

看起来我们缺少某些内容或者你的mysql数据库已关闭?