Jump to content

Recommended way of handling mysql connections in multiple files


toxicious

Recommended Posts

My website needs to do a lot of mysql connections to fetch and insert stuff into a db. In my last "version" of this website I have one file called dblogin.php in which I connect to the db. Then I just do an "include "dblogin.php" (kinda) and then I go on with the stuff. Is there any recommended way of doing this? I mean you want the address, username etc. in one file so you don't have to change it in multiple files if something changes.

Link to comment
Share on other sites

Most applications use a configuration file for this and other environment-dependent settings.Depending on the level of complexity of your application, you could either just include a file with variables/constants, like you're already doing, or you may include a file that returns an array/object that is your configuration, or you might use some sort of configuration file parser for a non-PHP file type (e.g. an ini file, an XML file, etc.).There is no recommended way of doing this, nor is there a most popular convention. Whatever you think will strike the balance between being easier (and flexible enough) for you and others to work with vs. your application easily reading and acting differently based on the configuration.

  • Like 1
Link to comment
Share on other sites

if the setting meant for developer hardcoding it in a php page (like in constants) would be sufficient. if it is large number of setting seprating it in a file would be a good choice. hardcoded settings are faster than loading a file.if the settings is for admin or third person or where is possibility to change the seetings frequently anytime or where you need an interface to edit the setting, storing it seperate file like xml,ini would be best choice. you need to just load the setting everytime.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...