Search code examples
phpmysqlquotes

Using double quotes vs single quotes when passing string as a parameter to function


i have a very strange problem, well not really a problem because i've fixed it but still, when i'm trying to connect to mysql db with:

mysql_connect("server", "user", "pass") or die(mysql_error());

im getting:

Access denied for user 'user'@'server' (using password: YES)

but when i change the quotes around the password to single quotes:

mysql_connect("server", "user", 'pass') or die(mysql_error());

it works just fine. i dont have this problem in another server i've got. so maybe it's something in the mysql settings or in the php.ini?

thanks.


Solution

  • You must have a $ in your password.

    That will cause php to interpolate a variable in the string.

    You might also have an escape sequence, e.g., \t in your password which will cause similar problems.

    Check out the manual for more info:

    http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing