Hioushi Posted May 6, 2010 Share Posted May 6, 2010 Hi everyone. I have a few years of programming experience (personal projects and such), but I'm relatively new to web programming, I'm in the process of learning PHP and with a C++ background this is going fairly well, but I'm having problems with a few concepts. The first thing I don't know how to implement, or if I should implement it this way, is a database connection class I'm working on. Basically, I wan't to access to the database through this class from many other different scripts by declaring an object on said script like so $db = new Database("server","db","user","psswd") $db->connect(); But I don't want to put the server info, db and all that on each script at the object declaration, because it will change. Is it ok to put this info in a text file and have class or whatever read the connection info from there to use it across the scripts? I'm thinking about some sort of ini or xml config file where settings like this could be stored, but I don't know what's the common/standard aproach in a web envioroment. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/200888-database-connection-script-and-other-questions/ Share on other sites More sharing options...
will35010 Posted May 6, 2010 Share Posted May 6, 2010 Just do an include of the db script and call the variable $db. [quote author=Hioushi link=topic=297073.msg1406980#msg1406980 date=1273153344] Hi everyone. I have a few years of programming experience (personal projects and such), but I'm relatively new to web programming, I'm in the process of learning PHP and with a C++ background this is going fairly well, but I'm having problems with a few concepts. The first thing I don't know how to implement, or if I should implement it this way, is a database connection class I'm working on. Basically, I wan't to access to the database through this class from many other different scripts by declaring an object on said script like so $db = new Database("server","db","user","psswd") $db->connect(); But I don't want to put the server info, db and all that on each script at the object declaration, because it will change. Is it ok to put this info in a text file and have class or whatever read the connection info from there to use it across the scripts? I'm thinking about some sort of ini or xml config file where settings like this could be stored, but I don't know what's the common/standard aproach in a web envioroment. Thank you. [/quote] Quote Link to comment https://forums.phpfreaks.com/topic/200888-database-connection-script-and-other-questions/#findComment-1054083 Share on other sites More sharing options...
Hioushi Posted May 6, 2010 Author Share Posted May 6, 2010 Yes, but I'd like to know if it's ok for the Database class to retrieve the connection details like server, database and user from a xml config or ini file so that I can separate the database class implementation from the actual connection parameters. Quote Link to comment https://forums.phpfreaks.com/topic/200888-database-connection-script-and-other-questions/#findComment-1054087 Share on other sites More sharing options...
PFMaBiSmAd Posted May 6, 2010 Share Posted May 6, 2010 It would be customary to put all application configuration settings in to a config.php (or similar name) file and include that at the beginning of an application. By putting php code (variables or defined constants) into a .php file, it is automatically protected against revealing the values should someone discover or guess the file name and request the file. If you use a .xml or .ini file, you must take additional steps to protect the contents. Once the variables or defined constants have been included, you just use them in the $db = new Database($server,$db,$user,$psswd) function call. Also, keep the class general purpose (pass the values in as parameters) so that it would be possible to create multiple instances of the class should you need to access more than one database in an application. Quote Link to comment https://forums.phpfreaks.com/topic/200888-database-connection-script-and-other-questions/#findComment-1054100 Share on other sites More sharing options...
Hioushi Posted May 6, 2010 Author Share Posted May 6, 2010 Thank you, that's what I was thinking about, leaving the class general purpose. Now, if I declare the connection parameters in the config.php file, like $server,$db,$user,$psswd, Shouldn't this variables need to be global in order to be available to other scripts? Quote Link to comment https://forums.phpfreaks.com/topic/200888-database-connection-script-and-other-questions/#findComment-1054109 Share on other sites More sharing options...
PFMaBiSmAd Posted May 6, 2010 Share Posted May 6, 2010 available to other scripts What other scripts? You would include the config.php file at the start of any web page that needs to use the configuration settings. Including a .php file is similar to including a .h file in C. You are including variables, defined constants, function definitions, and class definitions that are needed by the rest of the code on the page. A php include statement is essentially the same as copy/pasting the contents of the file being included into the source where the include statement is at and the contents exist in the same scope where the include statement is at. Quote Link to comment https://forums.phpfreaks.com/topic/200888-database-connection-script-and-other-questions/#findComment-1054119 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.