Elmo Watson (sp**********@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.