| | |
Set variables using php function
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 62
Reputation:
Solved Threads: 3
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
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
Then Call it by
Would this be correct.
Help in passing variables would be appreciated.
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)
$username = "";
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)
function &configDB($db) { if($db=='calendar') { $cfg[host] = "localhost"; $cfg[user] = "MyDBs"; $cfg[pass] = "Pass"; $cfg[database] = "MyDB1"; }else { $server = "localhost"; $dbusername = "MyDBs"; $dbpassword = "Pass"; $db_name = "MyDB2"; } }
Then Call it by
PHP Syntax (Toggle Plain Text)
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.
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:
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.
PHP Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- insert csv file into mysql through php (PHP)
- optional variables in php functions (PHP)
- php.ini confusion (PHP)
- php socket server - can't connect from flash xml client socket (PHP)
- Passing Variables (PHP)
- Equivalent ASP Snippet for PHP Spam Trap (ASP)
- Passing variables to and from PHP (JavaScript / DHTML / AJAX)
- Function variables (PHP)
- Php With Sessions Aah (PHP)
Other Threads in the PHP Forum
- Previous Thread: does anyone know to do this?
- Next Thread: printing last result of recordset
| Thread Tools | Search this Thread |
# 5.2.10 access alexa apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date directory display dissertation dynamic echo echo$_get[x]changingitintovariable... email encode error fairness file files folder form forms function functions google href htaccess html image images include indentedsubcategory insert ip javascript joomla legislation limit link local login mail memberships menu mlm multiple multipletables mysql mysqlquery newsletters oop open paypal pdf persist php problem provider query radio random recursion remote rss script search server sessions sms sockets source space spam sql syntax system table tutorial update upload url validator variable video web youtube





