How not to display an instance of a widget dynamically?

How to change the CSS style for an instance of a multi instance widget dynamically depending on the result of an SQL query?
When the query returns nothing at all, I would like that the instance of the widget is not displayed. But if the query returns a result, I would that this result is displayed normally in the instance.

I much sought, in vain.
Would someone have an idea?

Solutions Collecting From Web of "How not to display an instance of a widget dynamically?"

The description of your problem is very minimal but the obvious solution is simply to output widget data only when the query is not empty:

class cond_widget extends WP_Widget {

  public function __construct() {
    // widget actual processes  
    parent::__construct(
        'test', // Base ID
        'test', // Name
        array( 'description' => __( 'test', 'foo'))
    );
  }

  public function widget( $args, $instance ) {

    $qry = $wpdb->get_var('whatever');
    if (!empty($qry)) {
      echo $args['before_widget'];

      if (!empty($instance['title'])) {
        $title = $instance['title'];
        echo $args['before_title'].$title.$args['after_title'];
      }

      echo '<ul class="recent_pages">'; 

      echo '</ul>';
      echo $args['after_widget'];
    }
  }

}
add_action( 
  'widgets_init', 
  function(){
     register_widget( 'cond_widget' );
  }
);