How to display a specific category using a custom Query in WordPress?

Why are you using $wpdb to query from database. Use WP_Query or get_posts instead. This is how you can query from WordPress database and get 4 latest posts from category id 24.

<?php

    $args = array(
        'post_type' => 'post',
        'cat' => 24,
        'posts_per_page' => 4,
        'ignore_sticky_posts' => 1
    );

    $my_query = new WP_Query( $args );

    if ( $my_query->have_posts() ) :

        while ( $my_query->have_posts() ) : $my_query->the_post();
            get_template_part( 'content', get_post_format() );
        endwhile;

    endif;

?>

error code: 523