Skip to content

Commit 3292771

Browse files
authored
Change mysql CLI call to native PHP function (#158)
* Change mysql CLI call to native PHP function * Silent phpcs to allow for MySQL functions usage * Silence silencing errors. * Handle exception instead of supressing error * Add comment explaining the approach * Adjust label to make it more clear
1 parent d066642 commit 3292771

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

src/Config_Command.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,18 @@ public function create( $_, $assoc_args ) {
153153
WP_CLI::error( '--dbprefix can only contain numbers, letters, and underscores.' );
154154
}
155155

156-
$mysql_db_connection_args = [
157-
'execute' => ';',
158-
'host' => $assoc_args['dbhost'],
159-
'user' => $assoc_args['dbuser'],
160-
'pass' => $assoc_args['dbpass'],
161-
];
162-
163-
// Check DB connection.
156+
// Check DB connection. To make command more portable, we are not using MySQL CLI and using
157+
// mysqli directly instead, as $wpdb is not accessible in this context.
164158
if ( ! Utils\get_flag_value( $assoc_args, 'skip-check' ) ) {
165-
Utils\run_mysql_command( '/usr/bin/env mysql --no-defaults', $mysql_db_connection_args );
159+
// phpcs:disable WordPress.DB.RestrictedFunctions
160+
$mysql = mysqli_init();
161+
mysqli_report( MYSQLI_REPORT_STRICT );
162+
try {
163+
mysqli_real_connect( $mysql, $assoc_args['dbhost'], $assoc_args['dbuser'], $assoc_args['dbpass'] );
164+
} catch ( mysqli_sql_exception $exception ) {
165+
die( 'Database connection error (' . $exception->getCode() . ') ' . $exception->getMessage() );
166+
}
167+
// phpcs:enable WordPress.DB.RestrictedFunctions
166168
}
167169

168170
if ( ! Utils\get_flag_value( $assoc_args, 'skip-salts' ) ) {

0 commit comments

Comments
 (0)