Jump to content


Photo

mysql_select_db(); best practice. your thoughts?


  • Please log in to reply
6 replies to this topic

#1 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 24 August 2006 - 08:19 PM

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? 
"Confidence in the face of risk."

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 24 August 2006 - 08:32 PM

It makes no difference, unless you are working with multiple databases, then it is much easier to do the latter.

#3 laide234

laide234
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 24 August 2006 - 09:47 PM

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.

#4 jdforsythe

jdforsythe
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 24 August 2006 - 10:49 PM

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:

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

}

in that function connect to the server and select the database.

then in each of your pages:
include_once('functions.php');
access_db();

if your project uses more than one database on the same server, you could rewrite the access_db() function like so:

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

}

and when you need the database in your scripts, use:
access_db("dbname");

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.

#5 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 24 August 2006 - 10:58 PM

note that if you use multiple dbs you'll need to call access_db for each one prior to querying.


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.

#6 jdforsythe

jdforsythe
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 24 August 2006 - 11:32 PM

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.

#7 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 28 August 2006 - 02:28 PM

Thanks all for your replies!  ;D
"Confidence in the face of risk."




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users