After removing the new database connection from the function. This caused a bug that is fixed by using the global option in from of the variable to share it with the function.