Title: WRONG SQL SYNTAX
Last modified: September 1, 2016

---

# WRONG SQL SYNTAX

 *  Resolved [tripflex](https://wordpress.org/support/users/tripflex/)
 * (@tripflex)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/wrong-sql-syntax/)
 * All of your direct `$wpdb` query calls are incorrect. If you’re doing a select
   from a specific table, you should be using the `$wpdb` global, not specifically`
   wp_comments` or `wp_postmeta`
 * Example your incorrect code:
    `$most_sell_courses = $wpdb->get_results( "SELECT'
   meta_value', 'post_id' FROM 'wp_postmeta' WHERE 'meta_key' LIKE '%total_sales%'
   and meta_value > 0", OBJECT );`
 * Correct code:
    `$most_sell_courses = $wpdb->get_results( "SELECT 'meta_value','
   post_id' FROM $wpdb->postmeta WHERE 'meta_key' LIKE '%total_sales%' and meta_value
   > 0", OBJECT );`
 * Basically anywhere you are specifically using `wp_XXX` you need to use `$wpdb-
   >prefix` or just directly call `$wpdb->XXX` … reason being, if the standard `
   wp_` prefix is not used, your plugin will not function at all.
 * See documentation:
    [https://codex.wordpress.org/Class_Reference/wpdb](https://codex.wordpress.org/Class_Reference/wpdb)
 * [https://wordpress.org/plugins/woo-sensei-analytics/](https://wordpress.org/plugins/woo-sensei-analytics/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [tripflex](https://wordpress.org/support/users/tripflex/)
 * (@tripflex)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/wrong-sql-syntax/#post-7650064)
 * I had to replace any of the ticks in the SQL code above with a standard ‘ as 
   the ticks are used on here to create code blocks, but you should still get the
   point
 *  Plugin Author [WPExperts.io](https://wordpress.org/support/users/wpexpertsio/)
 * (@wpexpertsio)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/wrong-sql-syntax/#post-7650137)
 * Hi Tripflex!
 * Really appreciate for sharing this. You are absolutely right that this is really
   important thing which should not be neglect. We will fix and update this plugin
   soon.
 * Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘WRONG SQL SYNTAX’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/woo-sensei-analytics_edf0f7.svg)
 * [Analytics for Woo Sensei](https://wordpress.org/plugins/woo-sensei-analytics/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woo-sensei-analytics/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woo-sensei-analytics/)
 * [Active Topics](https://wordpress.org/support/plugin/woo-sensei-analytics/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woo-sensei-analytics/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woo-sensei-analytics/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [WPExperts.io](https://wordpress.org/support/users/wpexpertsio/)
 * Last activity: [9 years, 9 months ago](https://wordpress.org/support/topic/wrong-sql-syntax/#post-7650137)
 * Status: resolved