Home > Archive > MySQL Server Forum > August 2005 > DSN vs explicit connection









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author DSN vs explicit connection
Elmo Watson

2005-08-24, 9:23 am

I'm new in the PHP/MySQL arena - -
which is more common - to use DSNs or to implicitly code the connection
strings in the pages?


Malcolm Dew-Jones

2005-08-24, 1:23 pm

Elmo Watson (sputnik75043@No.Spam.Hotmail.com) wrote:
: I'm new in the PHP/MySQL arena - -
: which is more common - to use DSNs or to implicitly code the connection
: strings in the pages?

DSNs?

I use a small php script called Database.php that creates a global
variable $dbh and makes sure the correct database is selected. That
script is stored outside of the web tree, and usually has the login
details hardcoded, though it can read them from a config if needed. The
script is very small so I treat the entire things as part of the
configuration data. Also, it doesn't set any variables except the handle,
so a script that uses the database script can not accidently use the login
variables and expose them.

Each main php script can then simply say

include_once("Database.php");

and afterwards it can use "$dbh" in database calls. I find that method
most convenient.

Many people store the username, password, and other connect information in
a seperate file which they require and then use within their main
script.

include_once("LoginDetails.php"); # OR
include_once("AppConfigDetails.php");

$dbh=mysql_pconnect(
$host,$user,$pass) or die "naughty database";
# etc.

Either way, I wouldn't recommend putting the connection details in the
body of the main script. At the very least, seperating them makes it
easier to have multiple copies of the script that access different
databases, for testing etc.

--

This programmer available for rent.
Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2009 droptable.com