gpt4 book ai didi

使用 require_once() 函数时 PHP 和 Mysql 连接错误?

转载 作者:行者123 更新时间:2023-11-29 06:33:26 24 4
gpt4 key购买 nike

config.php

<?
// Database Constants
define("DB_SERVER", "localhost");
define("DB_USER", "gallery");
define("DB_PASS", "phpOTL123");
define("DB_DATABASE", "photo_gallery");
?>

database.php

<?php
require_once('config.php');
class MySQLDatabase
{
private $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());
}
}
}
$database=new MySQLDatabase();
$database->open_connection();
?>

我正在定义 DB_SERVER 和所有其他的,如您在上面看到的,但由于某种原因,我收到以下错误:

Warning: Use of undefined constant DB_SERVER - assumed 'DB_SERVER' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\photo_gallery\includes\database.php on line 6

Warning: Use of undefined constant DB_USER - assumed 'DB_USER' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\photo_gallery\includes\database.php on line 6

Warning: Use of undefined constant DB_PASS - assumed 'DB_PASS' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\photo_gallery\includes\database.php on line 6

Warning: Use of undefined constant DB_NAME - assumed 'DB_NAME' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\photo_gallery\includes\database.php on line 6

Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\photo_gallery\includes\database.php on line 6

Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\photo_gallery\includes\database.php on line 6 Database connection failed: mysqli_error()

最佳答案

PHP 短标签为 recommended against ,因为它们通常是关闭的。

因此,您的 <?没有执行任何操作,并且您的数据库凭据将被视为纯文本而不是 PHP。将其更改为完整的<?php修复它。

关于使用 require_once() 函数时 PHP 和 Mysql 连接错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55108092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com