Fatal error: Call to a member function query() on a non-object in my ajaxpage

The problem is that you are not loading WordPress, so none of it’s included framework is defined (in this particular case the global $wpdb; object and it’s methods).

WordPress has built in way of handling ajax requests. In particular you should send the data to the url: admin_url('admin-ajax.php'). Included in the sent data should be the variable action (and which should be unique), i.e. action:'my_action_to_do_something'.

WordPress will then fire one of two hooks:

 'wp_ajax_my_action_to_do_something' //For logged in users
 'wp_ajax_noprov_my_action_to_do_something'//For non-logged in users

You should hook a callback function on to one or both of those hooks (depending if the action should be performed for logged in users, non-logged in users or both). The sent data can be obtained via $_REQUEST.

There are loads of AJAX related questions on this site, so I would recommend reading through some of them. Also, see the Codex pages on ajax in plug-ins.