Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    include(); from DOCUMENT_ROOT not working inside of a function

    Hi all!

    I am having a problem including a file inside of a function.

    This works :

    PHP Code:
    function queryDB($query)
    {
    include 
    '../includes/config.php';

    And this does not :

    PHP Code:
    $configFileLocation=$_SERVER['DOCUMENT_ROOT']."/includes/config.php";
    function 
    queryDB($query)
    {
    include 
    $configFileLocation;

    Neither does this :

    PHP Code:
    function queryDB($query)
    {
    include 
    $_SERVER['DOCUMENT_ROOT']."/includes/config.php";

    If the config file is included using the $configFileLocation variable outside of the function, the variables in the config.php file are available. When included inside of the function (using via $configFileLocation), they are not.

    What am I missing?

    Thanks a lot.
    -DR.Wong

    Wheres the food at?

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Do you get your actual document root for
    PHP Code:
    echo $_SERVER['DOCUMENT_ROOT']; 
    Check the output of the above and then find out whether
    PHP Code:
    $_SERVER['DOCUMENT_ROOT']."/includes/config.php"
    becomes the actual path to that file.

    And this does not :

    PHP Code:
    $configFileLocation=$_SERVER['DOCUMENT_ROOT']."/includes/config.php";
    function 
    queryDB($query)
    {
    include 
    $configFileLocation;

    In the above code, the variable $configFileLocation won't get scope inside that function, unless you declare it as a global variable .
    Last edited by abduraooft; 01-25-2012 at 08:13 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    DR.Wong (01-25-2012)

  • #3
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    Yes, the $_SERVER['DOCUMENT_ROOT'] value is correct.

    Here is a better explanation :

    config.php
    PHP Code:
    //DATABASE CONFIGURATION
    $dbUsername="FOO";
    $dbPassword="BAR";
    $dbIP="localhost";
    $database="myDataBase"
    test.php
    PHP Code:
    $configFileLocation=$_SERVER['DOCUMENT_ROOT'].'includes/config.php';
    include 
    $configFileLocation;

    echo 
    $dbUsername;

    //This works, echos FOO

    function myFunction()
    {
    include 
    $configFileLocation;

    echo 
    $dbUserName;

    }

    myFunction();

    //This does not work, echos nothing and the function which actually uses the db info returns an error "no database selected". 
    -DR.Wong

    Wheres the food at?

  • #4
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    Sill mistake.

    I was declaring $configFileLocation outside of the function and then trying to use it inside of the function.

    Solved.
    -DR.Wong

    Wheres the food at?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •