Vinny
Vinny

Reputation: 151

Can't select columns from database, constant already defined

I'm facing a problem when I try to select a db from my database.

I'm using this code:

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']; 
}

And it returns these errors:

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

It's happening because I had to include the db.php (file which makes the connection to the sql db) on the header, but now I need it included on my sidebar, to display database data into it, is there a way I can connect to the database without these errors?

Upvotes: 1

Views: 430

Answers (1)

alexander.polomodov
alexander.polomodov

Reputation: 5524

You should use include_once instead of include, because your file with defining of constants should be included only once.

Change your code to this:

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']; 
}

Upvotes: 5

Related Questions