Sunday, March 25, 2018

PHP error - Constant DB_HOST already defined?



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.
I have this stuff working, only problem is that when I click on the nav link, I have these errors:




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



I have a config.php file that i use to establish a DB connection:




     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 my file that calls the config.php file which is causing the error:



   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');
?>










A small amount of simple code, but what is the problem? It is not a fatal error, but man, is it annoying.


Answer



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.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...