cordoprod Posted July 13, 2008 Share Posted July 13, 2008 Hello. If i have a page called index.php, and i include all my pages by $_GET. And it's always the same DB. Do i need to connect on the included pages as well? I know i need different queries though. And also, in a function do i need to connect to the db again there? And is it important to close the db? Quote Link to comment https://forums.phpfreaks.com/topic/114557-connecting-to-db/ Share on other sites More sharing options...
JonnyThunder Posted July 13, 2008 Share Posted July 13, 2008 I'm not sure where $_GET comes into it. But here's a rough guide... if you include a file one of these ways.... include('myfile.php'); include_once('myfile.php'); require('myfile.php'); require_once('myfile.php'); ... then any variables or resources which have been set in these files, will be usable in your current script. If say, you had one page with a link on which called a second page, as soon as the browser loads the second page - your resources do not carry across. A function cannot natively see a database connection created outside the function. Perhaps you could look into mySQL database classes. They are fairly easy to use and tidy things up a bit. I'd say it's fairly important to close databases, though it will happen by itself. I expect that PHP closes DB connections when the script terminates anyway! Quote Link to comment https://forums.phpfreaks.com/topic/114557-connecting-to-db/#findComment-589066 Share on other sites More sharing options...
budimir Posted July 13, 2008 Share Posted July 13, 2008 The best way is to make a file, where you specify your database connection, like: $db_server = "localhost"; $db_database = ""; $db_user = ""; $db_pass= ""; // Veza na bazu $veza = mysql_connect($db_server,$db_user, $db_pass) or DIE ('Nisam se uspio spojiti na server'); $veza_baza = mysql_select_db($db_baza,$veza) or DIE ('Nisam uspio odabrati bazu'); And then you include that file on every page where you need to do some queries. And in the end of that file where you have included your connection file you close your connection. mysql_close($name of connection); Quote Link to comment https://forums.phpfreaks.com/topic/114557-connecting-to-db/#findComment-589075 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.