select query working but breaks the following app