<!---- Database File --->
<?php
require_once 'config.php';
class MySQLDatabase {
private $connection;
function __construct() {
$this->open_connection();
}
public function open_connection() {
$this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if(!$this->connection) {
die("Database connection Failed: " .mysqli_error($this->connection));
}else {
$db_select = mysqli_select_db($this->connection, DB_NAME);
if(!$db_select) {
die("Database connection Failed: " .mysqli_error($this->connection));
}
}
}
public function close_connection() {
if(isset($this->connection)) {
mysqli_close($this->connection);
unset($this->connection);
}
}
public function query($sql) {
$result = mysqli_query($sql, $this->connection);
$this->confirm_query($result);
return $result;
}
private function confirm_query($result) {
if(!$result) {
die("Database connection Failed: " .mysqli_error($this->connection));
}
}
}
$database = new MySQLDatabase();
$database->close_connection();
?>
<!----Config FIle which ---->
<?php
defined('DB_SERVER') ? null : define("DB_SERVER","localhost");
defined('DB_USER') ? null : define("DB_USER", "faizy");
defined('DB_PASS') ? null : define("DB_PASS", "faizy");
defined('DB_NAME') ? null : define("DB_NAME", "photo_gallery");
?>
<!---index.php---->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php
require_once '../includes/database.php';
if(isset($database)){ echo "true"; } else { echo "false"; }
echo "<h1>Working</h1>";
?>
</body>
</html>
以下是我的浏览器显示的警告和通知,任何人都可以建议为什么会这样,当我使用单个php并尝试连接它连接时,但当我使用不同的函数文件并使用OOP php时总是这些错误是显示,任何人都可以帮助解决这些警告和通知
Notice: Use of undefined constant DB_SERVER - assumed 'DB_SERVER' in C:\xampp\htdocs\demo\oop\includes\database.php on line 13
Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in C:\xampp\htdocs\demo\oop\includes\database.php on line 13
Notice: Use of undefined constant DB_PASS - assumed 'DB_PASS' in C:\xampp\htdocs\demo\oop\includes\database.php on line 13
Notice: Use of undefined constant DB_NAME - assumed 'DB_NAME' in C:\xampp\htdocs\demo\oop\includes\database.php on line 13
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\demo\oop\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\demo\oop\includes\database.php on line 13
Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\demo\oop\includes\database.php on line 15
Database connection Failed:
答案 0 :(得分:1)
我刚刚使用三个单独文件中的所有上述代码进行了测试,这一切都适合我。我改变的只是数据库名称,并传递给我的数据库之一。您必须链接到错误的文件。
答案 1 :(得分:0)
将常量定义置于数据库逻辑之上,它将起作用。这将确认您的需求路径配置不正确。