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 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts

    mysqli_connect with arrays not in a function

    Ok, i may be thinking of Java or i may be wrong but can't you call arrays from inside functions without passing them in? Like this

    $food['soup'] = "wet";
    $foot['carrot'] = "hard";

    function makeFood(){
    ... play with the arrays or whatever
    }

    doesn't that work? Or is there a way to pass in arays with out having to pass them in.... man its late, ha.

    This is what i'm trying to do. I've setup my database variables in a file called conf.php and thers also a function in there called goDB(); goDB() connects to my database using the variables from the array in the file. I call goDB() from other files by importing conf.php into them. I want to be able to just call goDB() and dont worry about passing in the array.

    Thanks for the help. Oh, and after thinking about it, no i was wrong, in Java arays are special because they don't create a copy of themselves in a function when passed in but you still need to pass them in, I belive PHP has the same rule. so I'm going at this wrong, obviously.

    thanks for the help.

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You can, has to do with variable scope. In your function either use the global keyword or use $GLOBALS.

    e.g:
    PHP Code:
    function makeFood()
    {
        global 
    $food$foot;
        echo 
    '<pre>';
        
    print_r($food);
        
    print_r($foot);
        echo 
    '</pre>';
    }

    // or
    function makeFood()
    {
        echo 
    '<pre>';
        
    print_r($GLOBALS['food']);
        
    print_r($GLOBALS['foot']);
        echo 
    '</pre>';

    Ref:
    http://www.zend.com/manual/language.variables.scope.php

    Good luck;

  • #3
    New Coder
    Join Date
    Aug 2006
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you very much. that is very helpful but now i'm getting errors as if I made my arrays wrong, i don't think I have but heres what they look like

    global $gs_database['host'] = "localhost"; // database host ( for example 'localhost' )
    global $gs_database['database'] = "guildspace"; // database name
    global $gs_database['username'] = "root"; // database user
    global $gs_database['password'] = ""; // database user's password

    Heres the error

    Parse error: parse error, unexpected '[', expecting ',' or ';' in conf.php on line 3

    note, that first line i posted is line 3.


    I've also tried telling my function to use thoes vars as globals as the link you gave me said by using this line:

    global $gs_database['host'],$gs_database['username'],$gs_database['password'],$gs_database['database'];

    and it still give me the same error.
    Last edited by skinner927; 10-09-2006 at 06:20 AM.

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Close, use global on the array itself, not individual elements:
    PHP Code:
    global $gs_database;
    $gs_database['host'] = "localhost"// database host ( for example 'localhost' )
    $gs_database['database'] = "guildspace"// database name
    $gs_database['username'] = "root"// database user
    $gs_database['password'] = ""// database user's password 
    Good luck;

  • #5
    New Coder
    Join Date
    Aug 2006
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Awesome!!!!

    Thank you so much, it works perfectly now. And puting in global $gs_database; is so much easier!

    I'll get a hang of this PHP stuff sooner or later.

  • #6
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Cool, you're welcome


  •  

    Posting Permissions

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