As an aside, for security reasons you should use a prepared statement.Even if it's server-side code and there is no any iteration with user data (they come as JSON)
Also, this is a decent usecase for scope(exit) but it should be put earlier in the function.
Am I right understand that `scope(exit)` should be always at top, otherwise it would not work (it's very strange because by the docs it's calling every time when function out of the scopes)?