当我尝试从数据库中选择数据库时,我遇到了问题。
我正在使用此代码:
include($_SERVER['DOCUMENT_ROOT'].'/central/inc/db.php');
$SQLSelect = $odb -> query("SELECT * FROM `status_testadores` ORDER BY `ID` DESC");
while ($show = $SQLSelect -> fetch(PDO::FETCH_ASSOC))
{
$rowID = $show['ID'];
$sv1_db = $show['server_1'];
}
它会返回这些错误:
Notice: Constant DB_HOST already defined in /Applications/MAMP/htdocs/central/inc/db.php on line 2
Notice: Constant DB_NAME already defined in /Applications/MAMP/htdocs/central/inc/db.php on line 3
Notice: Constant DB_USERNAME already defined in /Applications/MAMP/htdocs/central/inc/db.php on line 4
Notice: Constant DB_PASSWORD already defined in /Applications/MAMP/htdocs/central/inc/db.php on line 5
这种情况正在发生,因为我必须在标题上包含db.php(与sql db建立连接的文件),但现在我需要将它包含在我的侧边栏中,将数据库数据显示在其中,有没有办法我可以连接到数据库而没有这些错误吗?
答案 0 :(得分:5)
您应该使用include_once
而不是include
,因为定义常量的文件只应包含一次。
将您的代码更改为:
include_once($_SERVER['DOCUMENT_ROOT'].'/central/inc/db.php');
$SQLSelect = $odb -> query("SELECT * FROM `status_testadores` ORDER BY `ID` DESC");
while ($show = $SQLSelect -> fetch(PDO::FETCH_ASSOC))
{
$rowID = $show['ID'];
$sv1_db = $show['server_1'];
}