DSN vs explicit connection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Elmo Watson

    #1

    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?


  • Malcolm Dew-Jones

    #2
    Re: DSN vs explicit connection

    Elmo Watson (sputnik75043@N o.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("D atabase.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("L oginDetails.php "); # OR
    include_once("A ppConfigDetails .php");

    $dbh=mysql_pcon nect($host,$use r,$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.

    Comment

    Working...