I’m not able to get access to $wpdb [duplicate]

If you’re not using WP’s built in Ajax handlers, which includes things for you, you will need to include the WP core yourself. Try adding

define('WP_USE_THEMES', false);
global $wpdb;
require(BASE_PATH . 'wp-load.php');

to the top of your file. I should point out that the path to wp-load.php may not be the same and hard coding the path will make your code un-portable.