On Thu, 2008-06-19 at 15:54 +1200, Byron wrote: > Hi > > I was wondering if this is the correct way to reference a variable from a > file that has been included inside of a function. Also just general > static/global variable usage. > > For example: > -------- > Myqsql_functions.php > -------- > <?php > require("config.php") > > function ach_mysqlCon() > { > static $con = mysql_connect(global > $mysql_host,$mysql_user,$mysql_pass); > if (!$con) > { > die('Could not connect: ' . mysql_error()); > } > } > > function ach_mysqlCloseCon() > { > if($con) > { > mysql_close($con) > } > } > ?> > ---------------------------- > > And the variables are defined in config.php > > -------------- > config.php > -------------- > <?php > //Mysql vars > $mysql_host = "localhost"; > $mysql_user = "user@localhost"; > $mysql_pass = ""; > ?> > -------------------------- > > So my questions are.... will ach_mysqlCloseCon() recognise the $con > variable > in ach_mysqlCon() as it is declared as static? will ach_mysqlCon() > recognise > the variables included from config.php as they are declared global? is > this > all syntax'd properly? It's all wrong looking :/ Myqsql_functions.php -------------------- <?php require("config.php") function ach_mysqlCon( $init=true ) { static $con = null; if( $init && $con === null ) { $con = mysql_connect ( $GLOBALS['mysql_host'], $GLOBALS['mysql_user'], $GLOBALS['mysql_pass'] ); if( !$con ) { die( 'Could not connect: '.mysql_error() ); } } } function ach_mysqlCloseCon() { if( ($con = ach_mysqlCon( false )) ) { mysql_close( $con ) } } ?> ---------------------------- And the variables are defined in config.php -------------- config.php -------------- <?php //Mysql vars $GLOBALS['mysql_host'] = "localhost"; $GLOBALS['mysql_user'] = "user@localhost"; $GLOBALS['mysql_pass'] = ""; ?> -------------------------- > Also, how would I, for example, change $mysql_host from a form? > > Cheers and thanks for any and all assistance. You're creating a global database connection... considering you've gone for static var within the function, you'll only ever create one... so the host would need to be set before you ever call the connection function... but to address the question: <?php $GLOBALS['mysql_host'] = $_REQUEST['host']; ?> I've intentionally used request since I don't know if you would do it via $_POST or $_GET. Additionally, one would hope you'd check/filter/process the submitted value before being so careless as to assign it as I've done above. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php