config.php文件名不正确吗?
这些示例来自lynda php beyond basic config.phpconfig.php文件名不正确吗?,php,php4,Php,Php4,这些示例来自lynda php beyond basic config.php <?php defined('DB_SERVER') ? null : define("DB_SERVER", "localhost"); defined('DB_USER') ? null : define("DB_USER", "root"); defined('DB_PASS') ? null
<?php
defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER') ? null : define("DB_USER", "root");
defined('DB_PASS') ? null : define("DB_PASS", "");
defined('DB_NAME') ? null : define("DB_NAME", "photo_gallery");
?>
<?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());
}
}
}
public function close_connection(){
if(isset($this->connection)) {
mysql_close($this->connection);
unset($connection);
}
}
public function query($sql) {
$result = mysql_query($sql, $this->connection);
$this->confirm_query($result);
return $result;
}
private function confirm_query($result){
if (!$result) {
die("Database query failed: " . mysql_error());
}
}
}
$database = new MySQLDatabase();
?>
<?php
require_once("../includes/database.php");
if (isset($database)) {
echo "true";
} else {
echo "false";
}
?>
database.php
<?php
defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER') ? null : define("DB_USER", "root");
defined('DB_PASS') ? null : define("DB_PASS", "");
defined('DB_NAME') ? null : define("DB_NAME", "photo_gallery");
?>
<?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());
}
}
}
public function close_connection(){
if(isset($this->connection)) {
mysql_close($this->connection);
unset($connection);
}
}
public function query($sql) {
$result = mysql_query($sql, $this->connection);
$this->confirm_query($result);
return $result;
}
private function confirm_query($result){
if (!$result) {
die("Database query failed: " . mysql_error());
}
}
}
$database = new MySQLDatabase();
?>
<?php
require_once("../includes/database.php");
if (isset($database)) {
echo "true";
} else {
echo "false";
}
?>
index.php
<?php
defined('DB_SERVER') ? null : define("DB_SERVER", "localhost");
defined('DB_USER') ? null : define("DB_USER", "root");
defined('DB_PASS') ? null : define("DB_PASS", "");
defined('DB_NAME') ? null : define("DB_NAME", "photo_gallery");
?>
<?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());
}
}
}
public function close_connection(){
if(isset($this->connection)) {
mysql_close($this->connection);
unset($connection);
}
}
public function query($sql) {
$result = mysql_query($sql, $this->connection);
$this->confirm_query($result);
return $result;
}
private function confirm_query($result){
if (!$result) {
die("Database query failed: " . mysql_error());
}
}
}
$database = new MySQLDatabase();
?>
<?php
require_once("../includes/database.php");
if (isset($database)) {
echo "true";
} else {
echo "false";
}
?>
现在,当我尝试在浏览器上运行index.php时,出现了一个错误
错误是:
不推荐:在第80行的C:\xampp\php\PEAR\Config.php中,不推荐通过引用分配new的返回值
弃用:在第166行的C:\xampp\php\PEAR\Config.php中,通过引用分配new的返回值是弃用的
注意:在第13行的C:\xampp\htdocs\photo\u gallery\includes\database.php中使用未定义的常量DB\u SERVER-假定为“DB\u SERVER”
注意:在第13行的C:\xampp\htdocs\photo\u gallery\includes\database.php中使用未定义的常量DB\u USER-假定为“DB\u USER”
注意:在第13行的C:\xampp\htdocs\photo\u gallery\includes\database.php中使用未定义的常量DB\u PASS-假定为“DB\u PASS”
警告:mysql\u connect()[function.mysql connect]:php\u network\u getaddresses:getaddrinfo失败:未知此类主机。在第13行的C:\xampp\htdocs\photo\u gallery\includes\database.php中
警告:mysql_connect()[function.mysql connect]:[2002]php_network_getaddresses:getaddrinfo失败:未知此类主机。(正在尝试通过tcp://DB_SERVER:3306)在第13行的C:\xampp\htdocs\photo\u gallery\includes\database.php中
警告:mysql\u connect()[function.mysql connect]:php\u network\u getaddresses:getaddrinfo失败:未知此类主机。在第13行的C:\xampp\htdocs\photo\u gallery\includes\database.php中
数据库连接失败:php\u network\u getaddresses:getaddrinfo失败:不知道这样的主机
我以某种方式通过重命名config.php修复了这个错误
所以我的问题是为什么我会犯这个错误?config.php文件名是问题所在吗?
Lynda php beyond basics的视频教程没有出现此错误。可能是由于同一目录中存在“config.php”和“config.php”文件。 在windows中,文件名不区分大小写。
在linux中,它们是。您的本地配置存在某种错误,可能是因为您安装了一些第三方PEAR,因此出现错误消息: 弃用:在第166行的C:\xampp\php\PEAR\Config.php中,通过引用分配new的返回值是弃用的 您应该修改系统使用的软件包和库,因为此错误消息表示代码过时(已弃用)。就个人而言,我在Windows上使用XAMPP,我的项目的htdocs文件夹中也有
config.php
,没有任何类似的错误。问题是链接的文件是为。在我的系统中,第166行内容如下:
$this->container = new Config_Container('section', 'root');
config.php
是调用配置文件的常规通用名称,没有问题。用这个名字保存它不会降低任何安全壁垒,只需重命名它就可以修复这些安全壁垒
如果项目成功加载配置文件,则其他错误将被修复。此外:
警告:
请不要使用mysql.*
函数来编写新代码。它们不再得到维护,社区已开始恢复。看到了吗
相反,您应该了解并使用或。应该给出一些关于决定使用哪个API的细节。对于PDO,这里有一个例子
你确定database.php和config.php都在同一个目录中吗?第166行和第80行的C:\xampp\php\PEAR\config.php似乎表明包含路径中还有另一个config.php?我认为这与文件名无关。“不推荐”似乎是因为php版本,“注意”是因为没有定义变量。“警告”是由于数据库连接中的错误。。您确定您的文件包含正确吗。@user1393669:开发人员仔细阅读错误消息是一个好习惯。如果再次尝试,您将看到另一个文件的确切路径:
C:\xampp\php\PEAR\Config.php