phpmysqlpdomysql-error-1045

underscore in php db variable causing problems


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)


Solution

  • 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