How to convert a mysqli query to wpdb

Hello, I’d appreciate some help in converting this mysqli query to using wpdb:

    $ret = array( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 );
    $q = "SELECT id,user,status,completed,track,coursecost,testcost," .
         "gst,pst,discount FROM exams WHERE student=$id AND status=" .  
         $theExam->STATUS_COMPLETED . " ORDER BY completed";

    if (!( $result = mysqli_query($Connection, $q) ) ) {
        if ($show ) {
            makeInstruction(
                4, "Database Error: Could not retrieve " .
                "student's exams: $q" 
            );
        }

        return( $ret );
    }

From my reading of the developer reference it should look something like:

global $wpdb;
    $q = $wpdb->get_results( "SELECT id,user,status,completed,track,coursecost,testcost," .
         "gst,pst,discount FROM {$wpdb->prefix}exams WHERE student=$id AND status=" .  
         $theClass->STATUS_COMPLETED . " ORDER BY completed");

How do I handle the error if the foregoing is the correct approach?

    if (!( $result = mysqli_query($Connection, $q) ) ) {
        if ($show ) {
            makeInstruction(
                4, "Database Error: Could not retrieve " .
                "student's exams: $q" 
            );
        }

        return( $ret );
    }

Thanks.



Source link