943,917 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 3599
  • PHP RSS
Jan 29th, 2009
0

Set variables using php function

Expand Post »
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
PHP Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  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

PHP Syntax (Toggle Plain Text)
  1. configDB('main');

Would this be correct.

Help in passing variables would be appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
PomonaGrange is offline Offline
67 posts
since Jun 2008
Jan 29th, 2009
0

Re: Set variables using php function

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:

PHP Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 948
Sarcastic Poster
ardav is offline Offline
6,698 posts
since Oct 2006
Jan 29th, 2009
0

Re: Set variables using php function

Thanks
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
PomonaGrange is offline Offline
67 posts
since Jun 2008
Jan 29th, 2009
0

Re: Set variables using php function

Damn! Forgot to code tag the examples. Sorry!
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 948
Sarcastic Poster
ardav is offline Offline
6,698 posts
since Oct 2006
Jan 29th, 2009
0

Re: Set variables using php function

Click to Expand / Collapse  Quote originally posted by ardav ...
Damn! Forgot to code tag the examples. Sorry!
Not a problem, I caught your gist...

Thanks again.
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
PomonaGrange is offline Offline
67 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: does anyone know to do this?
Next Thread in PHP Forum Timeline: Any more strtotime bugfixes ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC