As noted elsewhere, your createDatabaseConnection is failing to return an object, potentially because it is failing in general. The first thing you should do is look at that function and the possible returned object - especially in the case where connecting to the database fails. Without seeing the function its return state is unclear, but it might suffice to make sure that $DB is not NULL (Edit: From the code you've now posted you should check against it being false)
As a general check, you might use is_object to check that you have an object.