micah1701 Posted August 24, 2006 Share Posted August 24, 2006 which is the best way to make my database connection.I use a config file that has the mysql_select_db(); in it and I include the config file in any page that makes database connections.To connect to the database, should I specify the database in the config file using mysql_select_db();ORshould i just specify the database in the query, IE "SELECT * FROM database.tablename"your thoughts? Pro / Cons? Quote Link to comment https://forums.phpfreaks.com/topic/18578-mysql_select_db-best-practice-your-thoughts/ Share on other sites More sharing options...
hitman6003 Posted August 24, 2006 Share Posted August 24, 2006 It makes no difference, unless you are working with multiple databases, then it is much easier to do the latter. Quote Link to comment https://forums.phpfreaks.com/topic/18578-mysql_select_db-best-practice-your-thoughts/#findComment-80023 Share on other sites More sharing options...
laide234 Posted August 24, 2006 Share Posted August 24, 2006 I use a config file, especially when I have mutiple pages accessing the same database. Connecting on every page can be a hassle. I just include the config file. Quote Link to comment https://forums.phpfreaks.com/topic/18578-mysql_select_db-best-practice-your-thoughts/#findComment-80066 Share on other sites More sharing options...
jdforsythe Posted August 24, 2006 Share Posted August 24, 2006 what i do is this:create an external functions.php file (kind of like your config file you talk about)in that i create a function (which i now just copy+paste for each new project and modify the user/password/etc) called access_db(). here's an example:[code]function connect_db() { $DB_HOST = "localhost"; // mySQL local database host address $DB_LOGIN = "dbuser"; // database username $DB_PASSWORD = "dbpass"; // database password $DB_NAME = "dbname"; // database name /* connect to mysql server */ mysql_connect($DB_HOST, $DB_LOGIN, $DB_PASSWORD) OR die('Error: ' . mysql_error()); /* select database */ mysql_select_db($DB_NAME) OR die('Error: ' . mysql_error());}[/code]in that function connect to the server and select the database.then in each of your pages:[code]include_once('functions.php');access_db();[/code]if your project uses more than one database on the same server, you could rewrite the access_db() function like so:[code]function connect_db($DB_NAME) { $DB_HOST = "localhost"; // mySQL local database host address $DB_LOGIN = "dbuser"; // database username $DB_PASSWORD = "dbpass"; // database password /* connect to mysql server */ mysql_connect($DB_HOST, $DB_LOGIN, $DB_PASSWORD) OR die('Error: ' . mysql_error()); /* select database */ mysql_select_db($DB_NAME) OR die('Error: ' . mysql_error());}[/code]and when you need the database in your scripts, use:[code]access_db("dbname");[/code]note that if you use multiple dbs you'll need to call access_db for each one prior to querying.i find it easier to use external functions for things like this because otherwise your scripts look cluttered when you're going back to look at them, which makes them harder to decipher.there really is no difference between the two ways except using external functions makes your code easier to read and you can reuse the same functions.php for multiple projects with very little modification. it also makes it easier if you have many different php files on a project to just type access_db(); instead of writing the code every time (and if you change the name of the database, username, etc you only have to change it in one place!)any functions that are used over and over again, in the same site or multiple sites, should be put in seperate php files. that's just my opinion, though. Quote Link to comment https://forums.phpfreaks.com/topic/18578-mysql_select_db-best-practice-your-thoughts/#findComment-80088 Share on other sites More sharing options...
hitman6003 Posted August 24, 2006 Share Posted August 24, 2006 [quote]note that if you use multiple dbs you'll need to call access_db for each one prior to querying.[/quote]Everytime you call that function would create a new connection to the database, which can be very taxing on resources. If you need to use multiple databases, and don't want to specify the database in the query, then just use the mysql_select_db function again to change databases using the same connection. Quote Link to comment https://forums.phpfreaks.com/topic/18578-mysql_select_db-best-practice-your-thoughts/#findComment-80091 Share on other sites More sharing options...
jdforsythe Posted August 24, 2006 Share Posted August 24, 2006 oops! thanks @ hitman for correcting that!he's right. if you use multiple databases you'd want to omit the select_db completely from your access_db() function and just select the database wherever you need to switch inside your scripts. Quote Link to comment https://forums.phpfreaks.com/topic/18578-mysql_select_db-best-practice-your-thoughts/#findComment-80105 Share on other sites More sharing options...
micah1701 Posted August 28, 2006 Author Share Posted August 28, 2006 Thanks all for your replies! ;D Quote Link to comment https://forums.phpfreaks.com/topic/18578-mysql_select_db-best-practice-your-thoughts/#findComment-81629 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.