Have working sql query… trying to adjust it to use $wpdb

Your error message means that $wpdb in your code is currently pointing to a null pointer. Either WP is not loaded, or you’re missing a simple global $wpdb; statement, or both.

If you need to load WP, include wp-load.php.

Or better, in your JS, use ajaxurl:

url = ajaxurl + '?action=youraction'

alongside (assuming the above code):

add_action('wp_ajax_youraction', 'yourcode');

there’s an unauthenticated version of the same (see wp-admin/admin-ajax.php)