That Code makes no sense at all
1) membergroupids is availale in this Hook as $this->fetch_field('membergroupids'), no need for a Query.
2) Same goes for field5: $this->fetch_field('field5')
3) You don't have to execute Queries to set fields, just use $this->set('fieldname', 'value')
4) $vbulletin isn't within the Scope, $db also
Didn't I post Code that does smth. very similar for you in another Thread?