Title: &#8220;error in your SQL syntax&#8221;
Last modified: April 9, 2022

---

# “error in your SQL syntax”

 *  [isarisar](https://wordpress.org/support/users/isarisar/)
 * (@isarisar)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/error-in-your-sql-syntax-9/)
 * Another error that’s been popping up in our logs for months:
 * It’s from `product-add-ons-woocommerce/includes/Model/Group.php` lines 348ff.
   The `$categories` variable contains nothing, and so in only filled in as empty
   string.
 *     ```
       WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') OR p.product_id IN (4433487) OR g.apply_to = 'all'
              GROUP BY g.id
              OR...' at line 5 for query
              SELECT g.*, GROUP_CONCAT(DISTINCT c.category_id) as <code>categories</code>, GROUP_CONCAT(DISTINCT p.product_id) as <code>products</code>
                      FROM wp_za_groups as g
              LEFT JOIN wp_za_categories_to_groups as c ON c.group_id = g.id
              LEFT JOIN wp_za_products_to_groups as p ON p.group_id = g.id
                      WHERE c.category_id IN () OR p.product_id IN (4433487) OR g.apply_to = 'all'
              GROUP BY g.id
              ORDER BY g.priority ASC
       from require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/Divi/includes/builder/frontend-builder/theme-builder/frontend-body-template.php'), get_footer, do_action('get_footer'), WP_Hook->do_action, WP_Hook->apply_filters, et_theme_builder_frontend_override_footer, et_theme_builder_frontend_override_partial, require_once('/themes/Divi/includes/builder/frontend-builder/theme-builder/frontend-footer-template.php'), wp_footer, do_action('wp_footer'), WP_Hook->do_action, WP_Hook->apply_filters, et_fb_wp_footer, et_fb_enqueue_assets, do_action('et_fb_enqueue_assets'), WP_Hook->do_action, WP_Hook->apply_filters, et_fb_backend_helpers, et_fb_get_dynamic_backend_helpers, et_fb_current_page_params, et_fb_current_page_woocommerce_components, ET_Builder_Module_Woocommerce_Upsells::get_upsells, et_builder_wc_render_module_template, woocommerce_upsell_display, wc_get_template, include('/plugins/woocommerce/templates/single-product/up-sells.php'), wc_get_template_part, load_template, require('/plugins/woocommerce/templates/content-product.php'), do_action('woocommerce_after_shop_loop_item_title'), WP_Hook->do_action, WP_Hook->apply_filters, woocommerce_template_loop_add_to_cart, wc_get_template, include('/plugins/woocommerce/templates/loop/add-to-cart.php'), WC_Product_Variable->add_to_cart_text, apply_filters('woocommerce_product_add_to_cart_text'), WP_Hook->apply_filters, ZAddons\Frontend\Shop->add_to_cart_text, ZAddons\Frontend::hasTypes, ZAddons\Model\Group::getByProduct
       ```
   
 * Version info:
    PAOfWC: 2.1.38 WC 6.3.1 WP 5.9.3 PHP 8.0.17

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

 *  Thread Starter [isarisar](https://wordpress.org/support/users/isarisar/)
 * (@isarisar)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/error-in-your-sql-syntax-9/#post-15543383)
 * Right, so this occurs when adding a new product and is from `includes/Frontend/
   Product.php`‘s call of `Group::getByProduct( $product, true );` near line 439
   in `function wc_tab_addons`.
 * It’s the only time you call `includes/Model/Group.php`‘s function `getByProduct`
   with `true` as second parameter (`$single`), but you never observe the latter’s
   value, instead `$single` is unconditionally overwritten near line 327…
    `$single
   = get_post_meta($id, '_zaddon_disable_global', true) === "yes";` … and that’s
   always `false` for entirely new product objects?
 * So seems like you forgot that `$single` ought to be `||`‘ed with itself (or `
   |=`‘ed rather than `=`‘ed) in that assignment?
 *  Thread Starter [isarisar](https://wordpress.org/support/users/isarisar/)
 * (@isarisar)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/error-in-your-sql-syntax-9/#post-15545059)
 * There is yet another path this same error will occur: If I open a category site
   in Divi Theme Builder, I see it triggered six times _in addition_ to the actual
   products listed (for which it runs fine). The call path is:
    1. `includes/Frontend/Shop.php`: `add_to_cart_text` calls `Frontend::hasTypes( 
       $product )`
    2. `includes/Frontend.php`: `hasTypes` calls `Group::getByProduct($product)`
 *  Thread Starter [isarisar](https://wordpress.org/support/users/isarisar/)
 * (@isarisar)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/error-in-your-sql-syntax-9/#post-15545418)
 * Right, so rather than guessing around for ages & praying there aren’t even more
   bugged paths, I just rewrote the part in `getByProduct` to
    1. actually observe `$single` as parameter
    2. use the `$single` query if `$categories` is empty
 * Dunno if that’s as intended but it can’t be worse than the bugs, which lead to
   no query being executed at all.
 * The section of code is now:
 *     ```
       	$query = "
       		SELECT
       			g.* , GROUP_CONCAT(DISTINCT c.category_id) as <code>categories</code>, GROUP_CONCAT(DISTINCT p.product_id) as <code>products</code>
       		FROM {$table} g
       			INNER JOIN {$p2g} AS p ON (p.group_id = g.id)
       			LEFT JOIN {$p2g} pf ON (pf.group_id = g.id AND p.product_id <> pf.product_id)
       			LEFT JOIN {$c2g} c ON (c.group_id = g.id)
       		WHERE pf.product_id IS NULL AND c.group_id IS NULL AND p.product_id IN {$related_ids}
       		GROUP BY g.id
       		ORDER BY g.priority ASC
       		";
   
               if (!($single || get_post_meta($id, '_zaddon_disable_global', true) === "yes")) {
                   $product = $product->is_type('variation') ? wc_get_product($product->get_parent_id()) : $product;
                   $categories = $product->get_category_ids();
                   if ($categories) {
                       $categories = array_map('intval', $categories);
                       $categories = '(' . implode(',', $categories) . ')';
                       $query = "
       		SELECT g.*, GROUP_CONCAT(DISTINCT c.category_id) as <code>categories</code>, GROUP_CONCAT(DISTINCT p.product_id) as <code>products</code>
       			FROM {$table} as g
       		LEFT JOIN {$c2g} as c ON c.group_id = g.id
       		LEFT JOIN {$p2g} as p ON p.group_id = g.id
       			WHERE c.category_id IN {$categories} OR p.product_id IN {$related_ids} OR g.apply_to = 'all'
       		GROUP BY g.id
       		ORDER BY g.priority ASC
       		";
                   }
               }
       ```
   

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

The topic ‘“error in your SQL syntax”’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/product-add-ons-woocommerce_e9f5f9.
   svg)
 * [Product Add-Ons for WooCommerce](https://wordpress.org/plugins/product-add-ons-woocommerce/)
 * [Support Threads](https://wordpress.org/support/plugin/product-add-ons-woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/product-add-ons-woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/product-add-ons-woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/product-add-ons-woocommerce/reviews/)

 * 3 replies
 * 1 participant
 * Last reply from: [isarisar](https://wordpress.org/support/users/isarisar/)
 * Last activity: [4 years, 1 month ago](https://wordpress.org/support/topic/error-in-your-sql-syntax-9/#post-15545418)
 * Status: not resolved