WordPress 4.9.4自定义脚本连接外部数据库异常排查求助
Hey there, let's dig into this issue you're facing with your external database connection in WordPress 4.9.4. Since Ari Adminer confirms the connection works, the problem is almost certainly in how your script handles the connection or executes logic after that point. Here are actionable steps to troubleshoot:
WordPress often suppresses errors that could be causing your script to halt abruptly. Add these lines to your wp-config.php file to log errors privately:
define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', false );
Check the wp-content/debug.log file after running your script—this will likely show a specific error (like invalid query syntax, missing permissions, or a typo in a field name) that's stopping execution.
If you're using WordPress's wpdb class for the external connection, make sure you're not overwriting the default database object and that you're checking for errors at every step. Try this simplified test script:
// Initialize a separate wpdb instance for the external database $external_db = new wpdb( 'YOUR_DB_USER', 'YOUR_DB_PASS', 'YOUR_DB_NAME', 'YOUR_DB_HOST' ); // Confirm connection success if ( $external_db->last_error ) { die( 'Connection failed: ' . $external_db->last_error ); } echo 'Connected successfully!'; // Run a simple test query to check execution flow $test_results = $external_db->get_results( "SELECT * FROM your_target_table LIMIT 1" ); if ( $external_db->last_error ) { die( 'Query failed: ' . $external_db->last_error ); } var_dump( $test_results );
If the script stops after "Connected successfully!", the test query is where the failure happens—use the last_error output to fix it.
Sometimes WordPress's debug settings miss low-level PHP errors (like syntax issues or undefined functions). Add these lines at the top of your custom script to make all errors visible:
error_reporting( E_ALL ); ini_set( 'display_errors', 1 );
This will immediately show any fatal errors that are causing the script to crash before it can complete.
Your hosting or WordPress might be enforcing strict execution limits that cut your script short. Add this line to your script to temporarily extend the time limit:
set_time_limit( 300 ); // 5 minutes, adjust as needed
Also, verify your hosting provider doesn't have hard limits on memory or execution time that you need to adjust via their control panel.
Even if Ari Adminer sees the tables, confirm:
- The database user your script uses has the required permissions (SELECT/INSERT/UPDATE, depending on what you're doing) for the target tables.
- Your script uses exact, case-sensitive table/field names (Linux servers with MySQL are case-sensitive for table names).
If your script is running within a theme or plugin, try testing it in a standalone file (with WordPress loaded) to eliminate hook/filter interference. Add this at the top of the standalone file to access WordPress functions:
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
Once you run through these steps, you should have a clear idea of what's stopping your script. If you find a specific error message, feel free to share it and I can help narrow it down further!
内容的提问来源于stack exchange,提问作者Coolen

