in reply to can't call method 'Sql' on an undefined value

You have created the variable $connection but not assigned it a value. Should you be using $data instead?