Your code looks correct to me, so I suspect there is some kind of issue connecting to the second database.
Try the following which will log the last error:
$wpdb->show_errors();
if($wpdb->last_error !== '') :
$wpdb->print_error()
endif;
If you are using multisite, then you will also need to add:
define( 'DIEONDBERROR', true );
Have a look here for more info: https://codex.wordpress.org/Class_Reference/wpdb#Show_and_Hide_SQL_Errors