Use `$this->db` insteadof `$db` and remove `global $db;` above