gpt4 book ai didi

php - 常量问题和连接到数据库 PHP

转载 作者:行者123 更新时间:2023-11-30 23:49:14 26 4
gpt4 key购买 nike

我在屏幕上看到这些错误

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

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

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

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

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

这是我的代码:

<?php
require_once("config.php");

class MySQLDatabase {

private $connection;

function __construct() {
$this->open_connection();
}

public function open_connection() {
$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
if (!$this->connection) {
die("Database connection failed: " . mysql_error());
} else {
$db_select = mysql_select_db(DB_NAME, $this->connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
}
}

我的常量是这样定义的:

<?php

// Database Constants
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", "photo_gallery");

?>

我做错了什么?

最佳答案

我遇到了同样的错误,并通过一个小的编辑修复了它。顺便说一句,你应该迁移到 mysqli 而不是 mysql。

这是我的问题:PHP constant not defined

你应该用下面的代码替换database.php中的require_once

require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . "config.php");

它将路径设置为绝对路径,并且可以顺利正常地工作

关于php - 常量问题和连接到数据库 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20123993/

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