简体   繁体   中英

Using $_SERVER['DOCUMENT_ROOT'] means I can't access variables in an included file

I'm calling a database config file (db_config.php) in my header.php file. I'm calling header.php in my template (location.php) I need to access the variables within the db config file as I'm running queries that return a result, which I am then calling in the template.

So in my db_config.php file I have the following:

$area_query = "SELECT * FROM `locations` where `type` = 'area'";

mysqli_query($db, $area_query) or die('Error querying database.');

$area_result = mysqli_query($db, $area_query);

if (!$area_result) {
  printf("Error: %s\n", mysqli_error($con));
  exit();
}

Then in a file called locations.php I am looping through the result of the above using:

            while ($row = mysqli_fetch_array($area_result))
        {
            echo "<li class='location-list-item'><a href='location/$row[name]'><span class='initial-letter'>".mb_substr($row['name'],0,1)."</span>".$row['name']." <i class=\"fa fa-chevron-circle-right\" aria-hidden=\"true\"></i></a></li>";
        }

This works fine if I include the db_config.php in header.php using the following:

include("config/db_config.php");

However if I use:

include($_SERVER['DOCUMENT_ROOT']."/config/db_config.php");

It finds the db_config.php as expected (as echoing something in db_config.php outputs when visiting locations.php file) but I can't access $area_result. I get the following error:

Notice
: Undefined variable: area_result
on line
28


Warning
: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null 
given in
locations.php
on line
28

I'm confused as to why using $_SERVER['DOCUMENT_ROOT'] prevents me from accessing the variable? I need to use $_SERVER['DOCUMENT_ROOT'] for file path reasons.

To clarify, in locations.php I am including header.php which in turn is including db_config.php. I can echo stuff in db_config that outputs when visiting locations.php but I can't access the variables.

Thank you.

I would first suggest you echo the returned value of $_SERVER['DOCUMENT_ROOT'] to ensure it shows the path you expect it to. Do this in a local environment, of course.

<?php echo $_SERVER['DOCUMENT_ROOT']; ?>

Your config directory (for part of the path used for include()) needs to be directly inside your Document Root directory for that path to work, which is why checking to see what path is returned first will help you with this.

You may need to adjust your path to more accurately match where the file you want to include is located.

<?php include($_SERVER['DOCUMENT_ROOT'] . "/SOME_DIR/config/db_config.php"); ?>

Another option for you is if you want to include that file in multiple files in multi-depth directories, then you can also form a relative path using double dots like so...

<?php include('../config/db_config.php'); ?>

The above would give you access to the config/db_config.php file located above the current directory, and the following would give you access to the file two directories above the current directory your current file is in.

<?php include('../../config/db_config.php'); ?>

As a side note, it might be better to use include_once to avoid multiple including of the file, or even better, require_once which lets you use what's in the file without actually compiling it as part of that file.

One last thing you could try, although I'm not sure if this only applies to Object Orientated Programming (OOP), but you can try marking the variable as global before you use it.

<?php

global $area_result;

while ($row = mysqli_fetch_array($area_result)) {
    echo "<li class='location-list-item'><a href='location/$row[name]'><span class='initial-letter'>".mb_substr($row['name'],0,1)."</span>".$row['name']." <i class=\"fa fa-chevron-circle-right\" aria-hidden=\"true\"></i></a></li>";
}

?>

Hopefully I've helped you out and solved your problem. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM