Using wordpress function to retrieve data

By default $wpdb->get_results() returns results in the form of an array of objects. If you want an array of associative arrays instead, just do this:

$results = $wpdb->get_results( "...", ARRAY_A );

Obviously I’m not familiar with your project or the code you’re using, and I haven’t tested this, but you’d probably want to do something like the following:

$results = $wpdb->get_results(
    "SELECT rating_postid FROM {$wpdb->ratings}",
    ARRAY_A
);

if ( ! empty( $results ) ) {

    header( 'Content-Type: text/csv; charset=utf-8' );
    header( 'Content-Disposition: attachment; filename=data.csv' );

    $fp = fopen( 'php://output', 'w' );

    fputcsv( $fp, array_keys( $results[0] ) );

    foreach ( $results as $row ) {
        fputcsv( $fp, $row );
    }

    fclose( $fp );

}