wp-config.php中MySQL主机名语法解析错误排查求助
Hey there, let's troubleshoot that syntax error you're hitting. The error points to line 41 (your DB_HOST config), but looking at the code you shared, that line looks correct at first glance. Here are the most common fixes to try:
Check for hidden special characters
Sometimes copy-pasting files or encoding issues can sneak in invisible characters (like full-width semicolons, non-breaking spaces, or UTF-8 BOM markers) that PHP can't parse correctly. Even if the line looks fine, these hidden characters will throw errors.
Grab a plain-text editor (Notepad++, VS Code, etc.), turn on "show all characters" mode, and inspect line 41 and the lines around it. If you spot any weird symbols, delete them and retype the line manually:define('DB_HOST', 'localhost');Make sure you're using half-width (English) quotes and semicolons—full-width characters from other languages will break PHP.
Verify file encoding
If your wp-config.php is saved in an encoding other than UTF-8 (without BOM), PHP might misinterpret the code. In your editor, check the encoding setting:- In VS Code: Click the encoding label in the bottom-right corner and select "UTF-8".
- In Notepad++: Go to the "Encoding" menu and choose "UTF-8 without BOM".
Save the file after changing the encoding and test again.
Double-check nearby lines
Sometimes the line number in the error can be off (due to hidden lines or comments). Take a close look at the lines right before line 41—specifically the DB_PASSWORD definition:define('DB_PASSWORD', '');Ensure the quotes and semicolon here are also valid half-width characters, no typos or missing symbols.
Recreate the config section
If the file got corrupted during backup/transfer, try rewriting the entire MySQL settings block from scratch. Replace the existing config lines with this clean code:// ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define('DB_NAME', 'new_test_db'); /** MySQL database username */ define('DB_USER', 'root'); /** MySQL database password */ define('DB_PASSWORD', ''); /** MySQL hostname */ define('DB_HOST', 'localhost'); /** Database Charset to use in creating database tables. */ define('DB_CHARSET', 'utf8'); /** The Database Collate type. Don't change this if in doubt. */ define('DB_COLLATE', '');Enable debug mode for more clues
If none of the above works, turn on WordPress debug mode to get more detailed error info. Find this line in wp-config.php:define('WP_DEBUG', false);Change it to:
define('WP_DEBUG', true);This will show additional errors that might point you to the real issue.
内容的提问来源于stack exchange,提问作者hims

