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 11 of 11
  1. #1
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    838
    Thanks
    15
    Thanked 9 Times in 9 Posts

    Global Array not Working?

    Alright in my config.inc.php I got
    PHP Code:
    $db["username"]    = "user123";
    $db["password"]    = "likeimleavingthisin";
    $db["host"]    = "localhost";
    $db["name"]    = "stats";
    $db["prefix"]    = "prefix"
    and in my scripts.class.php I have
    PHP Code:
    function login() {
            global 
    $db;
            echo 
    ">".$db["username"]."<";
            return 
    0;
        } 
    However when I call the function in a different page all I get is ><. I have had this happen before and couldn't explain it. It only happens with the arrays and not a variable like $db_user. What's wrong?
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #2
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    838
    Thanks
    15
    Thanked 9 Times in 9 Posts
    ^bumpers^
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    The code works...
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #4
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    838
    Thanks
    15
    Thanked 9 Times in 9 Posts
    Well, as I said... I only get ><. Unless there is a setting in PHP that is not allowing me to do it otherwise.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Is the assignment of $db['username'] in a function, and do you have global $db in that function?

    Put a print_r($db) at the top of login() and see what that gives you.

  • #6
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    838
    Thanks
    15
    Thanked 9 Times in 9 Posts
    The $db array values are in config.inc.php (which isn't a class and has not functions). The function wanting access to the array is in a class.

    I used the print_r and it returned a 1.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,961
    Thanks
    2
    Thanked 305 Times in 297 Posts
    Is the function just a function or is it part of a class definition? I think we need more information about the organization of your code, what files are included where? If this is part of a class definition, include enough of the code so that someone could duplicate and test this.

    I call the function in a different page
    Does this different page also directly include the config.inc.php or does it include a page that then includes the config.inc.php?

    Even though this issue should be solved to gain an understanding of why it is not working, these values are essentially constants and it would be better to use a define(...) statement. This will also avoid the scope/global issue. A defined constant can be used in any code that it has been included into with out the need for the global keyword within functions.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #8
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    838
    Thanks
    15
    Thanked 9 Times in 9 Posts
    I'll give a shot using the constants...
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #9
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,961
    Thanks
    2
    Thanked 305 Times in 297 Posts
    On a PHP5 system the orginal concept you described works as expected -

    main code -
    PHP Code:
    <?php
    include "config.inc.php"// include $db[...] variables
    include "scripts.class.php"// include the class definition

    $mabismad = new mabismad// create an instance of the class
    $mabismad->login(); // call the login() member function
    ?>
    The config.inc.php file -
    PHP Code:
    <?php
    $db
    ["username"]    = "user123"
    $db["password"]    = "likeimleavingthisin"
    $db["host"]    = "localhost"
    $db["name"]    = "stats"
    $db["prefix"]    = "prefix";  
    ?>
    The scripts.class.php file -
    PHP Code:
    <?php
    class mabismad {

    function 
    login() { 
            global 
    $db
            echo 
    ">".$db["username"]."<"
            return 
    0
        }
    }
    ?>
    Produces this output -
    Code:
    >user123<
    I suspect that if this did not work it is either because the config.inc.php file does not have <?php ?> tags or it was not included in the code where the instance of the class was used?
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #10
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    838
    Thanks
    15
    Thanked 9 Times in 9 Posts
    Well, it was included and called because when I had
    PHP Code:
    <?php 
    $db
    ["username"]    = "user123";  
    $db["password"]    = "likeimleavingthisin";  
    $db["host"]    = "localhost";  
    $db["name"]    = "stats";  
    $db["prefix"]    = "prefix";   
    $prefix "prefix";
    ?>
    the $prefix variable worked just as it's suppose to, it's just the $db array that doesn't want to seem to go global.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #11
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I used the print_r and it returned a 1.
    Well the array $db is getting stomped somewhere down the line then, which you probably don't need me telling you! Not something I could really debug without the actual code/environment... good luck.


  •  

    Posting Permissions

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