Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Abe Layee
Abe Layee
8,378 Points

Use of undefined constant DS_HOST

I'm getting this error "Use of undefined constant DS_HOST". My mysql connection to the database was working before I tried fetching data from the database. This is my code

  <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Developer's Nation</a>
        </div>
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
              <?php
                   $query = "SELECT * FROM categories";
                   $select_all_categories_query = mysqli_query($connect,$query);

                   while($row = mysqli_fetch_assoc($select_all_categories_query)) {
                    $cat_title =  $row['cat_title'];
                     echo "<li>{$cat_title}</li>";
                   }
               ?>
                <!-- <li>
                    <a href="#">About</a>
                </li>
                <li>
                    <a href="#">Services</a>
                </li>
                <li>
                    <a href="#">Contact</a>
                </li> -->
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

My database connection code is below

 <?php

   $db['db_host'] = 'localhost';
   $db['db_user'] = 'root';
   $db['db_pass'] = '*****************';
   $db['db_name'] = 'cms';

// loop throug the array and covert to constant

  foreach ($db as $key => $value) {
    definE(strtoupper($key),$value);
  }

   $connect = mysqli_connect(DS_HOST, DB_USER, DB_PASS, DB_NAME);

    if(!$connect) {
        die("Error connecting to the database");
    }


?>
Muhammad Ehsan Hanif
Muhammad Ehsan Hanif
8,236 Points

There is a typo here in this line $connect = mysqli_connect(DS_HOST, DB_USER, DB_PASS, DB_NAME);

Also make sure you are calling the vars you have defined above instead of some random vars.

1 Answer

Abe Layee
Abe Layee
8,378 Points

Thank and I didn't even realized it