I have a file that drives my reusable functions into a single file ( functions.php ). This is include_once() 'd on every page he needs. I get an error when my user functions try to access a MySQL connection outside their own scope. The source is a bit like this:
<?php // functions.php $connect = mysql_connect("localhost", "user", "pass") or die("MySQL said: ".mysql_error()); mysql_select_db("database", $connect) or die("MySQL said: ".mysql_error()); // no error /* ... */ function getmotd($user) { $query = "SELECT cid FROM `users` WHERE id = ".$user; $query = mysql_query($query, $connect); // error occurs here, $connect is not a valid MySQL link-resource /* ... */ } ?>
Why can't my function access variables declared above? I can get a successful connection by playing the $connect declaration inside the function.
Any insight on how I can get around this or what am I doing wrong here?
source share