Fix bugs on double sql queries