tvtg Posted March 7, 2010 Share Posted March 7, 2010 Hi, I want to keep "mysql_connect" result in a global that can be used by few scripts I have a file DBActivities.php which contains many functions that access my sql and run some queries so the file looks like that: <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $conn = ''; connect() { global $conn = mysql_connect($dbhost, $dbuser, $dbpass) or ...... } f1() { run some query using conn as global } f2() { run some query using conn as global } ?> when my main page is loaded I call connect with success meaning conn is now contains the DB connect result now I have a second php file Stuff.php <?php include DBActivities.php; f1(); f2(); ?> I assumed that conn is global and was initated then when calling to f1 and f2 conn keeps the information How do I maintain conn without keep connecting to the DB for every query Thanks in advance T. Quote Link to comment https://forums.phpfreaks.com/topic/194448-hold-a-global-with-database-connect/ Share on other sites More sharing options...
trq Posted March 7, 2010 Share Posted March 7, 2010 You will need to inlcude your connection code into any file that needs it. You cannot store $conn in any global. Quote Link to comment https://forums.phpfreaks.com/topic/194448-hold-a-global-with-database-connect/#findComment-1022789 Share on other sites More sharing options...
Mchl Posted March 7, 2010 Share Posted March 7, 2010 More specifically, none global variables are retained between different executions of different scripts. Add call to connect() to the end of your dbactivities.php file, and make sure it's included in every file that needs MySQL connection. Quote Link to comment https://forums.phpfreaks.com/topic/194448-hold-a-global-with-database-connect/#findComment-1022828 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.