Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-09-2006, 04:14 AM   PM User | #1
skinner927
New Coder

 
Join Date: Aug 2006
Posts: 96
Thanks: 1
Thanked 0 Times in 0 Posts
skinner927 is an unknown quantity at this point
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.
skinner927 is offline   Reply With Quote
Old 10-09-2006, 05:02 AM   PM User | #2
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
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;
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 10-09-2006, 05:16 AM   PM User | #3
skinner927
New Coder

 
Join Date: Aug 2006
Posts: 96
Thanks: 1
Thanked 0 Times in 0 Posts
skinner927 is an unknown quantity at this point
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 05:20 AM..
skinner927 is offline   Reply With Quote
Old 10-09-2006, 05:21 AM   PM User | #4
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
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;
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 10-09-2006, 05:24 AM   PM User | #5
skinner927
New Coder

 
Join Date: Aug 2006
Posts: 96
Thanks: 1
Thanked 0 Times in 0 Posts
skinner927 is an unknown quantity at this point
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.
skinner927 is offline   Reply With Quote
Old 10-09-2006, 05:25 AM   PM User | #6
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
Cool, you're welcome
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:07 AM.


Advertisement
Log in to turn off these ads.