Why do I get the error "Call to a member function on a non-object"?!

When using ezSQL within a function you must use the global keyword. Like so..

function my_function()
{
global $db;
$db->query("some query");
}