How to return a record from a custom table by sending an AJAX request

  • Your code (both client-side and server-side) makes no sense and is very ugly.
  • You’re not loading WordPress in your custom PHP script, so there’s no way $wpdb would work.
  • To call a method of an object, you should do $wpdb->get_results('... sql ...').

You shouldn’t call PHP scripts directly anyway. It’s better to create a REST endpoint or a rewrite endpoint inside your theme or plugin.