Can't Connect to MySQL with PHP: The Hostname Mismatch
When you encounter an "Access denied for user 'username'@'localhost'" error in a PHP script while phpMyAdmin works perfectly, the issue is rarely the password or the privileges granted to the database user. It is almost certainly a hostname mismatch caused by how the connection string is interpreted versus how the MySQL server records the user's host.
In your specific scenario using XAMPP on Windows XP, you are running PHP locally. You attempted to connect using mysql_connect('localhost', 'westbrookc16', 'megadots'). This command tells the MySQL server to authenticate the user specifically against entries in the user table where the Host column matches 'localhost'.
However, phpMyAdmin usually connects via a specific mechanism that bypasses this restriction. When you access phpMyAdmin through your browser on Windows XP, it typically connects to the default port (3306) using the standard socket or TCP/IP connection associated with the current machine's hostname. More importantly, phpMyAdmin often runs under a context where the user is identified as 'westbrookc16'@'localhost' successfully, but your PHP script might be triggering a different authentication flow if the socket path or host definition differs slightly.
The core issue here is that MySQL distinguishes between root@localhost, root@127.0.0.1, and root@%. If you created the user in phpMyAdmin using a generic host like % (any host) or if the server expects the connection via a specific socket file, the simple string 'localhost' in your PHP code might not map correctly to the internal MySQL user table entry.
To resolve this, you need to inspect the mysql.user table directly within your XAMPP environment. Open phpMyAdmin, navigate to the SQL tab, and run:
SELECT User, Host FROM mysql.user;
You will likely see an entry for westbrookc16 paired with a host like % or localhost. If your PHP code is failing despite the credentials being correct in phpMyAdmin, check if you are inadvertently connecting via a different socket. On Windows XP with XAMPP, the MySQL server might be configured to listen on a specific port or require a distinct socket file path for local connections.
If the user exists but the connection fails, verify that your PHP script is using the correct driver. The code snippet provided uses the legacy mysql_* functions (mysql_connect, mysql_error). These functions are deprecated as of PHP 5.5 and removed entirely in PHP 7.0+. While XAMPP on Windows XP likely runs an older version of PHP where these functions exist, relying on them is a significant technical debt. The connection logic itself is fragile because the legacy driver does not provide detailed error codes, often resulting in generic "Access denied" messages that obscure whether the issue is authentication or privilege escalation.
A more robust approach involves switching to the mysqli extension or PDO. Here is how you would refactor the connection using mysqli with explicit host verification:
$conn = new mysqli('localhost', 'westbrookc16', 'megadots', 'your_database_name');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
This provides a clearer error message if the host or credentials are incorrect. However, if you must stick to the legacy code for compatibility with your XP setup, ensure that the Host field in your MySQL user table matches exactly what PHP is sending. Sometimes, adding a specific entry for westbrookc16@% (allowing connections from any host) or ensuring the socket path is consistent between phpMyAdmin and PHP can resolve the discrepancy.
Finally, consider whether the issue lies with the XAMPP configuration rather than the code. The my.ini file controls how MySQL starts on Windows. If the server is configured to use a specific socket directory that differs from where PHP expects to find it, local connections might fail even with correct credentials. Always check the error log in xampp/mysql/data/error.log for specific messages like "Host 'westbrookc16' doesn't exist" or authentication plugin mismatches, which are common when upgrading clients but keeping legacy server versions.
For modern projects moving away from XAMPP on Windows XP, adopting a Laravel stack provides better security and connection management out of the box. You can find resources and migration guides at https://laravelcompany.com to help transition your development environment to a more secure and maintainable architecture.