The Problem
If the MySQL password contains a dollar sign character ‘$’, PHP is unable to connect and issues an error like:
Access denied for user 'username'@'hostname' (using password: YES)
Connections via other clients work correctly.
The Solution
PHP interprets dollar signs inside of double quotes as starting a variable name, which will be expanded. This expansion will change the password sent to MySQL.
There are 3 ways to avoid this problem:
- Escape the character with a backslash: “pa\$\$word” (actual password being pa$$word)
- Use single quotes: ‘pa$$word’
- Avoid using the ‘$’ character in the password.
To help detect this kind of problem, a good practice is to run PHP with error reporting enabled and set to include E_STRICT (or E_ALL for PHP versions 5.4.0 and later). This setting will notify you of an undefined variable, which may reveal the problem.
PHP Error Levels
PHP provides several built-in constants for describing different error levels. Table below includes some of the more important ones.
Name | Description |
---|---|
E_ERROR | Fatal run-time error. Execution is halted. |
E_WARNING | Non-fatal run-time error. |
E_NOTICE | Run-time notice about possible error. |
E_USER_ERROR | Fatal user-generated error. |
E_USER_WARNING | Non-fatal user-generated warning. |
E_USER_NOTICE | User-generated notice. |
E_COMPILE_ERROR | Fatal compile-time error. |
E_PARSE | Compile-time parsing error. |
E_STRICT | Suggested change to ensure forward compatibility. |
E_ALL | All errors, except E_STRICT prior to PHP 5.4. |