I don't see where GalleryTotal is used on the page. I also am not familiar with count(1) in a SQL statement, so I'm not sure why it is necessary. Can't you use the ->TotalRows attribute of the original query?
Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.
These out-of-the-box solutions provide you proven, tested applications that can be up and running now. Build a store, a gallery, or a web-based email solution.