我是一个相对较新的开发人员,但确实知道一些东西。
我正在做一个PHP基础教程,并试图制作数据库类。
在课堂上写完所有内容后,我试着看看是否建立了连接。
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 failed: No such host is known
如果我使用(下面的代码)而不是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…但我真的很困惑为什么这个方法不起作用:-/(我将改变一切到mysqli一旦我完成了DB类,因为它是教程。不,我没有权限访问练习文件)
听起来像是在你的config.php中,你在定义常量时遗漏了引号。
wrong:
define(DB_SERVER, "localhost");
right:
define("DB_SERVER", "localhost");
仔细检查,或者从config.php中发布相关部分:)
尝试用以下方式定义常量。
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
通知。