views:

41

answers:

2

I store all of my login information for databases in files outside of the public tree in variables such as

$hostname = '172.0.0.0';
$dbname = 'myname_mydbname';
$username = 'myname_user';
$pw = 'password';

That's pretty standard.

The problem is that this particular hosting I am working with requires the myname_ to be appended to the front of all databases and user names. When I store these strings and pass them to a PDO it drops everything in the username after myname, and drops the password string all together... If I put the username and password in the function as strings instead of variables everything works. I am at my wits end. can anyone help? here is the function as it is in code.

Does not work:

$this -> DB = new PDO ("mysql:host={$hostname}; dbname={$dbname}", $username, $pw);

works:

$this -> DB = new PDO ("mysql:host={$hostname};dbname={$dbname}", 'myname_user', 'password');

I am hoping someone here can make me feel stupid... thanks in advance. -David

the error might help...

Failed to get DB handle: SQLSTATE[28000] [1045] Access denied for user 'myname'@'localhost' (using password: NO)

A: 

Nothing I can find in the PDO documentation suggests that you can specify username or password in the DSN string - it is a "Database Source Name" not "Database Source Name and Authentication" String the fact you are using no password should be a hint to this, and the username being 'myname' is probably just because most RDBMs use the $USER environment var to connect if none is specified (which i must assume is set to 'myname')

i.e. i think you simply have to use the extra parameters to pass the authentication credentials

tobyodavies
Just checked - for *some* backends (e.g. postgreSQL) you can specify authentication in the DSN, but not mysql.
tobyodavies
Well, it seems the variables are actually just showing as null in the file that uses them. You are right about about it assuming the $USER because of it though. I am going to have to trouble shoot why the variables are null, I can echo a statement from the file they are stored in, so I know it is being included.
david
oops, i misread your question quite a bit :( is it inside a function that this is being called, do you have global $username? thats a trap i've fallen into many times
tobyodavies
No, I didnt, thanks.
david
A: 

As a work around I built get functions in the file that the information is stored that return the strings, and it works. Super strange, but it works.

david
$this -> DB = new PDO ("mysql:host=".getHost().";dbname=".getDBName(), getUserName(), getPW());
david