Well your default inbox is folderid 0. Do some debugging. Change this line:
PHP Code:
WHERE pm.userid=$userid AND pm.folderid=$folderid
To
PHP Code:
WHERE pm.userid=$userid AND pm.folderid='0'
If you do not get an error when you refresh the page then you need to make sure that the $folderid variable is holding some information. Look above your code and see if anything says $folderid = $var.