Set variables using php function

Thread Solved

Join Date: Jun 2008
Posts: 62
Reputation: PomonaGrange is an unknown quantity at this point 
Solved Threads: 3
PomonaGrange PomonaGrange is offline Offline
Junior Poster in Training

Set variables using php function

 
0
  #1
Jan 29th, 2009
Hi There folks.

I was thinking of trying to make a php function in my sites configuration file so that I could keep all mysql database info in one central file.

I should mention that this is the first time I have tried to make my own function.

I have the file started, but can't get it to work like I'd hoped.

Is it possible to set a variable like
  1. $username = "";
in a function and be able to insert that into a file.

I'd assumed that I would call the function and it would set the variable, but so far it hasn't worked.

Here is what I have
  1. function &configDB($db)
  2. {
  3. if($db=='calendar')
  4. {
  5. $cfg[host] = "localhost";
  6. $cfg[user] = "MyDBs";
  7. $cfg[pass] = "Pass";
  8. $cfg[database] = "MyDB1";
  9. }else
  10. {
  11. $server = "localhost";
  12. $dbusername = "MyDBs";
  13. $dbpassword = "Pass";
  14. $db_name = "MyDB2";
  15. }
  16. }

Then Call it by

  1. configDB('main');

Would this be correct.

Help in passing variables would be appreciated.
There are alot of people smarter than me, BUT at least I try to be of some help. Of coarse I'm not perfect, I need help too.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 989
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 129
ardav's Avatar
ardav ardav is online now Online
Posting Shark

Re: Set variables using php function

 
0
  #2
Jan 29th, 2009
Your function variables are LOCAL variables (only visible to the function). You usually have to RETURN a value from the function to pass the value to the outside. Create your complete connection variable in the function and then do:

  1. return $connection;

at the end of the function. Your example does nothing useful.

$db = 'whatever';
$conn = configDB($db);

This will then give you a your connection object in $conn and can be used to run queries:

$result = mysql_query('SELECT * FROM myTable',$conn);

However, you're hard-coding connection data (user + password). This is not recommended, although OK for testing.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 62
Reputation: PomonaGrange is an unknown quantity at this point 
Solved Threads: 3
PomonaGrange PomonaGrange is offline Offline
Junior Poster in Training

Re: Set variables using php function

 
0
  #3
Jan 29th, 2009
Thanks
There are alot of people smarter than me, BUT at least I try to be of some help. Of coarse I'm not perfect, I need help too.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 989
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 129
ardav's Avatar
ardav ardav is online now Online
Posting Shark

Re: Set variables using php function

 
0
  #4
Jan 29th, 2009
Damn! Forgot to code tag the examples. Sorry!
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 62
Reputation: PomonaGrange is an unknown quantity at this point 
Solved Threads: 3
PomonaGrange PomonaGrange is offline Offline
Junior Poster in Training

Re: Set variables using php function

 
0
  #5
Jan 29th, 2009
Originally Posted by ardav View Post
Damn! Forgot to code tag the examples. Sorry!
Not a problem, I caught your gist...

Thanks again.
There are alot of people smarter than me, BUT at least I try to be of some help. Of coarse I'm not perfect, I need help too.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC