Change MySQL PDO connection to a WPDB connection

I have an autofill search box with a PHP script that gets data from the DB.
It uses a PDO connection, and it works perfectly even with my WordPress Database, but since I’m not sure of how PDO works, I’d like to rewrite this with WordPress functions. Does anyone know how?

This is the autosuggest script:

if (isset($_GET['term'])){
    $return_arr = array();

    try {
        $conn = new PDO("mysql:host=".DB_SERVER.";port=8889;dbname=".DB_NAME, DB_USER, DB_PASSWORD."");
        $conn->exec("set names utf8");
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $stmt = $conn->prepare('SELECT supplier_company FROM wp_teleapo_supplier');
        $stmt->execute(array('term' => '%'.$_GET['term'].'%'));

        while($row = $stmt->fetch()) {
            $return_arr[] =  $row['supplier_company'];
        }

    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }


    /* Toss back results as json encoded array. */
    echo json_encode($return_arr);
}

Solutions Collecting From Web of "Change MySQL PDO connection to a WPDB connection"

You need to repalce the PDO connection object with WPDB object. And for the kind of query in your example, looks like the best way to do it is with $wpdb->getcol. Something like this (didn’t test):

if ( isset( $_GET['term'] ) ){
    global $wpdb;
    $term = filter_var( $_GET['term'], FILTER_SANITIZE_STRING, FILTER_NULL_ON_FAILURE );
    if ( is_null( $term ) ) {
        echo json_encode( array() );
    } 
    else {
        $return_array = $wpdb->get_col( 
             $wpdb->prepare( 
                 "SELECT supplier_company 
                 FROM {$wpdb->prefix}_teleapo_supplier 
                 WHERE term = %s",
                 $term
             ) 
        );
        $return_arr = $return_arr ? $return_arr : array();
        /* Toss back results as json encoded array. */
        echo json_encode( $return_arr );
    }
}