Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP error - Constant DB_HOST already defined?

Tags:

php

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 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

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

<?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:

<?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.

like image 618
Phill Cookie Avatar asked Nov 19 '11 23:11

Phill Cookie


2 Answers

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.

like image 148
Ondřej Mirtes Avatar answered Nov 15 '22 18:11

Ondřej Mirtes


Do either header.php or footer.php include config.php?

If so thats your problem.

like image 32
Toby Allen Avatar answered Nov 15 '22 19:11

Toby Allen