Conditional formatting on data fetched from MYSQL

First a few notes:

  • Shortcodes do accept arguments, so you could write [marks subject=”Biology”] (@see https://codex.wordpress.org/Shortcode_API)
  • You should never ever pass raw data directly into a SQL statement, WordPress uses PDO, this means you can prepare statements. (@see https://developer.wordpress.org/reference/classes/wpdb/prepare/)
  • Using the get_results will return an Array, object or null (@see https://developer.wordpress.org/reference/classes/wpdb/get_results/). There are many approaches here. You could go with $wpdb->get_var(), to get a single value. Or $wpdb->get_results() to fetch both in one query. I think we are missing some data, if you want a specific row just for one user, you could also use $wpdb->get_row(). Think that the database is a Excel sheet. If you want multiple rows and columns, you want to use $wpdb->get_results(). IF you want just one specific row you could use $wpdb->get_row(). IF you want just one column of data $wpdb->get_column(). If you just want one specific cell $wpdb->get_var().
  • I would go with the icon approach instead of the image approach if you just want to use a “checkmark” or some “exclamation” mark based on failing or passing a specific score. A nice and free icon library (@see https://fontawesome.com/)

The code based on your given question, although I think you’re missing a WHERE statement in your query to fetch a specific persons ‘marks’ based on own projects.

add_shortcode( "marks", function( $atts ) {

    //= defaults
    $args = shortcode_atts( array(
        'subject' => 'biology',
    ), $atts );

    //= database query
    global $wpdb;
    $query  = $wpdb->prepare( "SELECT prev, current FROM grade WHERE sub = %s", $args['subject'] );
    $result = $wpdb->get_row( $query );

    //= bail when no result
    if( is_null( $result ) ) return;

    //= conditional
    if( $result[ 'prev' ] < $result[ 'current' ] ) {
        echo "<p color=\"color: green;\">{$result['current']}<i class=\"far fa-check\"></i></p>";
        
    } else {
        echo "<p color=\"color: red;\">{$result['current']}<i class=\"far fa-times\"></i></p>";
        
    }
    
} );

If needed you can easily debug objects and arrays on screen with the following code:

echo var_export( $array, true );

Hope this helps out!