replaced direct SQL with wpdb queries

git-svn-id: https://plugins.svn.wordpress.org/wp-noexternallinks/trunk@1032659 b8457f37-d9ea-0310-8a92-e5e31aec5664