I can't be sure, due to the language (ish) barrier, however it appears you're trying to use string as the conditional to gid (gid = (string)) when it should be an int. Using intval - should help some however I won't know for sure without reviewing the code.
What do you need from me?