PHP 错误 - 常量 DB_HOST 已定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8198061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP error - Constant DB_HOST already defined?
提问by Phill Cookie
I am doing a little homework assignment in which we are making a very rudimentary CMS. We are to fill in a form containing title, body, permalink. The CMS then takes the permalink, and adds it to the main nav bar. When that permalink is clicked on the navbar, the title, content, datestamp created and datestamp modified are to be displayed.
我正在做一些家庭作业,其中我们正在制作一个非常基本的 CMS。我们将填写一个包含标题、正文、固定链接的表格。然后 CMS 获取永久链接,并将其添加到主导航栏。当在导航栏上单击该固定链接时,将显示标题、内容、创建的日期戳和修改的日期戳。
I have that stuff working, only problem is that when I click on the nav link,
我有那些东西在工作,唯一的问题是当我点击导航链接时,
I receive this notice:
我收到此通知:
Notice: Constant DB_HOST already defined in C:\Program Files\xampp\htdocs\php\assignment_6\config.php on line 2
Notice: Constant DB_USER already defined in C:\Program Files\xampp\htdocs\php\assignment_6\config.php on line 3
Notice: Constant DB_PASS already defined in C:\Program Files\xampp\htdocs\php\assignment_6\config.php on line 4
Notice: Constant DB_NAME already defined in C:\Program Files\xampp\htdocs\php\assignment_6\config.php on line 5
注意:常量 DB_HOST 已经在 C:\Program Files\xampp\htdocs\php\assignment_6\config.php 第 2 行定义
注意:常量 DB_USER 已经在 C:\Program Files\xampp\htdocs\php\assignment_6\config.php 第 3 行定义
注意:常量 DB_PASS 已经在 C:\Program Files\xampp\htdocs\php\assignment_6\config.php 第 4 行定义
注意:常量 DB_NAME 已经在 C:\Program Files\xampp\htdocs\php\assignment_6\config.php 第 5 行定义
I have a config.php file that I use to establish a DB connection:
我有一个用于建立数据库连接的 config.php 文件:
<?php
define('DB_HOST','******');
define('DB_USER','******');
define('DB_PASS','******');
define('DB_NAME','******');
$cms_db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if(!$cms_db){
echo"Could not connect: ".mysql_error();
}
?>
Here is the code from the file calling config.php
causing this notice:
这是config.php
导致此通知的文件调用中的代码:
<?php
require('config.php');
$perm = $_GET['p'];
$query = "SELECT * FROM cms WHERE permalink = '$perm'";
$result = $cms_db->query($query);
$row = $result->fetch_assoc();
$page_title = $perm;
require('header.php');
?>
<h1><?=$row['title'];?></h1>
<hr/><br/>
<p class="para"><?=$row['content']?></p>
<?php require('footer.php');?>
A small amount of simple code, but what is the problem? It is not a fatal error but it is annoying.
少量简单的代码,但是有什么问题呢?这不是致命错误,但很烦人。
回答by Ond?ej Mirtes
You're probably including config.php multiple times. Check all your scripts and find this duplicity. You can use require_once()
instead of require()
to prevent this.
您可能多次包含 config.php。检查您的所有脚本并找到这种口是心非。您可以使用require_once()
代替require()
来防止这种情况。
回答by Toby Allen
Do either header.php or footer.php include config.php?
header.php 或footer.php 是否包含config.php?
If so thats your problem.
如果是这样,那就是你的问题。