Hi, I am working on a php project which users multiple functions. All functions are written in a single file called functions.php. Assume I am calling one such function from index.php. Now, let me describe my problem: The function I am calling expects a database connection. I set my database connection in another file called dbconn.php and include that in both index.php and functions.php But, even after including (I am using require_once() function) the db connection file in both index.php and functions.php, I am not able to use the same in function. Even if I insert a require_once inside the function definition, it did not work for me. Could anyone please help in getting the global db connection configuration which can make it easy to modify if I move servers - otherwise, it is going to be too difficult to handle during server migration. Thanks in advance
@Oil3L Thanks for that. Could you please elaborate more on that with example. Google does not have enough details for that. what should I write in dbconn.php, functions.php and index.php Thanks
Ok let's say that inside dbconn.php you have this: $conn = mysql_connect($host,$username,$password); mysql_select_db($db_database,$conn); PHP: And after you include it, use this in a function: function tFunc($var) { global $conn; /* + Rest of the code */ } PHP: That should resolve your problem.
Another way would be to pass the connection in the function parameters, e.g. function tFunc($var, $conn) { /* + Rest of the code */ } Code (markup):