My guess is that you need to declare
global $wpdb;
prior to your PHP code.
Looks like you're using the Exec-PHP plugin. Make sure you use
caution in surfacing any PHP / database functionality. If the code makes use of visitor-supplied data (i.e. $_GET variables), then poorly-secured code can allow SQL injection and other exploits.