Home >Backend Development >PHP Tutorial >Why Does a Dollar Sign ($) in a Password Cause Database Connection Issues?
In a recent encounter, a PHP application experienced difficulties establishing a connection to a MySQL database. Despite using the correct credentials, the database remained inaccessible.
Investigation revealed that the password contained a dollar ($) sign:
$_DB["password"] = "mypas$word";
This caused the password to be truncated to "mypas" when sent to the database, leading to the connection failure.
The issue was resolved by escaping the dollar sign with a backslash:
$_DB["password"] = "mypas$word";
This allowed the password to be sent to the database correctly.
To avoid such issues, it's recommended to use single-quoted strings for database passwords:
$_DB['password'] = 'mypas$word';
Single-quoted strings are not processed and are interpreted as-is. This approach is faster and less prone to errors.
The above is the detailed content of Why Does a Dollar Sign ($) in a Password Cause Database Connection Issues?. For more information, please follow other related articles on the PHP Chinese website!