"select 1"
"1"?
So this is checking for existence?
I really don't like multiple conditions in an 'unless'.
Something about the fact it is reversing the test (in my head) that make additional booleans within it confusing.
I'd test for the bad $sid to start off with and not bother with a database call if it is not going to be used.
And I've seen FAR worse code.