replacing $wpdb->escape, which is deprecated in wp 3.6, with esc_sql - h...

...ttp://core.trac.wordpress.org/ticket/24774

The random edits are my editor removing trailing whitespace from lines.