Test for result before dereferencing it

I get an error about fetch_array() being called on a non object before applying this change.