For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > May 2004 > Re: Passing vars into functions









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Passing vars into functions
Janwillem Borleffs

2004-05-18, 4:31 pm

jaquito13 wrote:
> I am working on a script that connects to MySQL databases using a
> function. To initiate the function in the example below, I would type
> the code --------
> dbconnect('main').
> --------I then want it to parse the user, host, and password into the
> function from preset variables. I think that you get the picture.
>


You probably searching for something like:

$user = 'user';
$pass = 'pass';

function connect($database) {
// Import the $user and $pass variables into
// the function's namespace to use it there
global $user, $pass;
....
}

But it will make the function more flexible when you pass the credentials as
arguments.

Also, don't forget to either return the resource or store it in a global
variable.

> $dbconnect = mysql_pconnect(eval(" $host =
> "$host ";"),


Why are you using eval when you don't need to:

$dbconnect = mysql_pconnect($host = 'host', $user='user', $pass='pass');

* or *

$host = 'host';
$user = 'user';
$pass = 'pass';

$dbconnect = mysql_pconnect($host, $user, $pass);


JW



jaquito13

2004-05-18, 6:30 pm

Thanks for the help. The reason I was using eval is because someone on
another site told me that it would work, and I am also a Flash
programmer, so I am used to eval doing what I needed. Thanks.



----------------------------------------
The post originated from PHP Freaks:
----------------------------------------
http://www.phpfreaks.com
http://www.phpfreaks.com/forums


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com