Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Elmo Watson
August 24, 2005, 1:15 pm
rate this thread
Re: DSN vs explicit connection
: 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?
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
and afterwards it can use "$dbh" in database calls. I find that method
Many people store the username, password, and other connect information in
a seperate file which they require and then use within their main
include_once("LoginDetails.php"); # OR
$dbh=mysql_pconnect($host,$user,$pass) or die "naughty database";
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.
- » Americanas.com SALDO de TVs Com at 80% de Desconto (29798)
- — Newest thread in » MySQL Database Forum