Jump to content

mysql_select_db(); best practice. your thoughts?


micah1701

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.