Skip to content

Commit 7e177ee

Browse files
Copilotswissspidy
andauthored
Address code review: fix edge cases in run_query_via_mysqli()
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 62d9d98 commit 7e177ee

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

‎src/DB_Command.php‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,16 +1981,20 @@ protected function run_query_via_mysqli( $query ) {
19811981
$db_host = $parts[0];
19821982
if ( isset( $parts[1] ) ) {
19831983
$port_or_socket = trim( $parts[1] );
1984-
if ( '/' === $port_or_socket[0] ) {
1984+
if ( '' !== $port_or_socket && '/' === $port_or_socket[0] ) {
19851985
$socket = $port_or_socket;
1986-
} else {
1986+
} elseif ( '' !== $port_or_socket ) {
19871987
$port = (int) $port_or_socket;
19881988
}
19891989
}
19901990
}
19911991

1992+
// When using a socket, pass 0 for port. When a port is explicitly set, use it; otherwise use the default.
1993+
$port_val = null !== $port ? $port : 0;
1994+
$socket_val = null !== $socket ? $socket : '';
1995+
19921996
// phpcs:ignore WordPress.DB.RestrictedClasses.mysql__mysqli -- direct mysqli required as wpdb fallback when mysql binary is unavailable.
1993-
$conn = new mysqli( $db_host, DB_USER, DB_PASSWORD, '', null !== $port ? $port : 3306, null !== $socket ? $socket : '' );
1997+
$conn = new mysqli( $db_host, DB_USER, DB_PASSWORD, '', $port_val, $socket_val );
19941998

19951999
if ( $conn->connect_errno ) {
19962000
WP_CLI::error( sprintf( 'Failed to connect to the database: %s', $conn->connect_error ) );

0 commit comments

Comments
 (0)