Jump to content

Archived

This topic is now archived and is closed to further replies.

micah1701

mysql_select_db(); best practice. your thoughts?

Recommended Posts

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();

OR

should i just specify the database in the query, IE  "SELECT * FROM database.tablename"

your thoughts? Pro / Cons? 

Share this post


Link to post
Share on other sites
It makes no difference, unless you are working with multiple databases, then it is much easier to do the latter.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.