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 9 of 9
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    include not parsing variables

    i use an include
    PHP Code:
    require_once "ms_config/db.php"
    The path is correct (when i change the path i get an error).
    the file only contains four variables for this:

    PHP Code:
    $link mysql_connect($db_host,$db_user,$db_pw) or die (mysql_error());
    mysql_select_db($db_db$link) or die ('Error 002'); 
    it doesnt work. i have done the same in other scripts (on other websites) but some actually live on the same server (different virtual host). what could stop variables from being parsed? is there anything in apache/.htaccess that prevents parsing?

    thanks
    Last edited by rotezecke; 07-05-2011 at 10:30 AM.

  • #2
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,415
    Thanks
    269
    Thanked 32 Times in 31 Posts
    its actually

    Code:
    require_once("db.php");

    is ms_config in the root?

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by durangod View Post
    its actually

    Code:
    require_once("db.php");

    is ms_config in the root?
    require is a special construct, not a function; the brackets are optional.
    OP, if the path is registering as valid and not throwing an error, the only things I can think of that would cause this are:
    1. The file has been included with a non-filesystem wrapper. This would be essentially an http:// type link. The results are returned, and NOT the variables.
    2. The file is unsetting the variables before they are used. Can be either.
    3. The file is included AFTER you want to use the variables.
    4. The variables are not named the same.

    You also haven't specified WHAT your error is. Is it a PHP error? A SQL error? Just not doing as expected?

    After you have included the file, you can dump the $GLOBALS to see if it includes your specified variables and their values are correct.

  • #4
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,415
    Thanks
    269
    Thanked 32 Times in 31 Posts
    Fou-Lu, thanks for that see i learned something today, peace

  • #5
    New Coder
    Join Date
    Sep 2010
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post
    Thanks for your thoughts.
    I think i can rule out 2,3 and 4. Option 1 I dont quite understand yet but will look into it. I
    Quote Originally Posted by Fou-Lu View Post
    You also haven't specified WHAT your error is. Is it a PHP error? A SQL error? Just not doing as expected?

    After you have included the file, you can dump the $GLOBALS to see if it includes your specified variables and their values are correct.
    That's the problem, there is nothing to dump. I get an SQL error caused by 'empty variables' in the connect function. once i hardcode my username,password into my function it works fine. As I said, the include path is correct (when i change the path i get a php error - file not found). and i include the file immediately before i need the variables. i copied and pasted my variables to prevent spelling errors.

  • #6
    New Coder
    Join Date
    Sep 2010
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post

    Solved

    the problem was that the file was included/required elsewhere on the same page. however, i called it with require_once. changing it to require fixed my problem and variables are parsed.
    how would i get more appropriate error/warning messages for events like these?
    Last edited by rotezecke; 07-05-2011 at 08:11 PM.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by rotezecke View Post
    the problem was that filed was included/required elsewhere on the same page. however, i called it with require_once. changing it to require fixed my problem and variables are parsed.
    how would i get more appropriate error/warning messages for events like these?
    You cannot.
    Require versus require_once dictates if it will attempt to override the functionality or not. This will successfully override any variables, but no defined constants or structures including functions. If you have functions or structures in it as well, it will throw a fatal error.
    This would indicate you are changing something that really shouldn't be changed from that scope or location; a variable with configuration information. You really should only need to require_once (once being the keyword), and operate on copies of the variables provided instead via non-referenced functions. So this sounds to me that it fits in closest with #2 as a cause, but instead of an unset its overwritten. You can post your code if you want.

    As for #1, you would simply look for require 'http://site.com/path/script.php';. You cannot retrieve variables from an http wrapped include.

  • Users who have thanked Fou-Lu for this post:

    rotezecke (07-05-2011)

  • #8
    New Coder
    Join Date
    Sep 2010
    Posts
    20
    Thanks
    3
    Thanked 1 Time in 1 Post
    I use a content management system (CMSMS) and some modified plugin also needs this config file and calls it with require before my attempted require_once call which then failed. so posting the code would be a bit awkward. correct me if i am wrong: to prevent a second require i could define my variables as global before i import (require) them for my first script?

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That won't work no.
    As soon as you call an include, it imports any variables into the scope of the current symbols table. Typically, this will already be in the global scope of main(), so no data will be overwritten. We know this is the case as well since the variables have changed between the calls, and this wouldn't happen if it were called in a scoped object or method - the variables would then have been discarded as a part of the stack.
    Sounds to me like it was just a bad design of a plugin, and its modifying something it should not be. Your only option will be to continue using the require.


  •  

    Tags for this Thread

    Posting Permissions

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