WordPress activate theme error $pagenow

$pagenow is a global, just like you do with $wpdb later on in your code you should call global $pagenow; before trying to use it

and $_GET['action'] is only set when ?action= is in the url, this isnt always the case. Always make sure it is set before trying to use it:

if( isset($_GET['action']) && $_GET['action'] == 'activate' )