Title: Invalid SQL (report)
Last modified: April 19, 2025

---

# Invalid SQL (report)

 *  Resolved [Knut Sparhell](https://wordpress.org/support/users/knutsp/)
 * (@knutsp)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/invalid-sql-report/)
 * Just to test if this plugin had any effect, I installed it on a site that has“
   The Events Calendar” plugin active. There was only one event, a past event. The
   Events page then stopped displaying the past event(s) below the current (none).
   On the PHP error log this was written:
 * `WordPress database error You have an error in your SQL syntax; check the manual
   that corresponds to your MySQL server version for the right syntax to use near'))
   CAST( wp_tec_occurrences.start_date AS DATETIME ) AS event_date, CAST( wp_te'
   at line 1 for query SELECT COUNT( DISTINCT( wp_posts.ID, ) ) CAST( wp_tec_occurrences.
   start_date AS DATETIME ) AS event_date, CAST( wp_tec_occurrences.duration AS 
   DECIMAL ) AS event_duration
   FROM wp_posts JOIN wp_tec_occurrences ON wp_posts.
   ID = wp_tec_occurrences.post_idWHERE 1=1 AND wp_posts.post_type = 'tribe_events'
   AND ((wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private'))ORDER
   BY wp_tec_occurrences.start_date ASC, wp_tec_occurrences.duration ASC, wp_posts.
   post_date ASCmade by require('wp-blog-header.php'), require_once('wp-includes/
   template-loader.php'), include('/plugins/the-events-calendar/src/views/v2/default-
   template.php'), Tribe\Events\Views\V2\Template_Bootstrap->get_view_html, Tribe\
   Events\Views\V2\View->get_html, do_action('tec_events_before_view_html_cache'),
   WP_Hook->do_action, WP_Hook->apply_filters, TEC\Events\SEO\Controller->issue_noindex,
   TEC\Events\SEO\Controller->should_add_no_index_for_list_based_views, Tribe__Repository-
   >count, WP_Query->get_posts, apply_filters_ref_array('posts_pre_query'), WP_Hook-
   >apply_filters, TEC\Events\Custom_Tables\V1\WP_Query\Modifiers\Events_Only_Modifier-
   >filter_posts_pre_query, TEC\Events\Custom_Tables\V1\WP_Query\Custom_Tables_Query-
   >get_posts, WP_Query->get_posts, WP_Query->set_found_posts
 * As you can see there is a _trailing comma_ in the DISTINCT expression’s function
   parameter list, and a _missing comma_ after the COUNT expression, before the 
   CAST expression.
 * Each of these simple actions stops the error:
    1. Deactivating this plugin (no side effect)
    2. Deactivating The Events Calendar (lost functionality)
    3. Adding a snippet to a site plugin or theme `functions.php`:
 * `\add_filter( 'query', static function( string $query ): string {`
    `$query =\
   str_replace( '.ID, ) ) CAST', '.ID ) ), CAST', $query );` `return $query;}, 11);
 * Site Health Info:
   `wp-core version: 6.8 site_language: nb_NO user_language: nb_NO
   timezone: Europe/Oslo permalink: /%year%/%monthnum%/%day%/%postname%/wp-active-
   theme name: Neve (neve) version: 4.0.1 author: ThemeIslewp-plugins-active (2)
   The Events Calendar: version: 6.11.2, author: The Events Calendar Count Pagination
   Fix for MySQL: version: 1.1.3, author: Team iSidewp-server php_version: 8.3.6
   64bit pretty_permalinks: truewp-database extension: mysqli server_version: 8.0.36
    -  This topic was modified 1 year, 1 month ago by [Knut Sparhell](https://wordpress.org/support/users/knutsp/).
    -  This topic was modified 1 year, 1 month ago by [Knut Sparhell](https://wordpress.org/support/users/knutsp/).
      Reason: Typo and formatting

Viewing 1 replies (of 1 total)

 *  Plugin Author [jorisvst](https://wordpress.org/support/users/jorisvst/)
 * (@jorisvst)
 * [1 year ago](https://wordpress.org/support/topic/invalid-sql-report/#post-18476831)
 * Hi [@knutsp](https://wordpress.org/support/users/knutsp/),
 * Thank you for reporting this issue.
   We’ve just released a new version that includes
   a fix to prevent invalid MySQL identifiers and resolve the bad SQL syntax. Could
   you please let us know if the issue is now resolved on your end if you disable
   your custom snippet?Thanks!

Viewing 1 replies (of 1 total)

The topic ‘Invalid SQL (report)’ is closed to new replies.

 * ![](https://ps.w.org/count-pagination-fix/assets/icon.svg?rev=3124717)
 * [Count Pagination Fix for MySQL](https://wordpress.org/plugins/count-pagination-fix/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/count-pagination-fix/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/count-pagination-fix/)
 * [Active Topics](https://wordpress.org/support/plugin/count-pagination-fix/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/count-pagination-fix/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/count-pagination-fix/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [jorisvst](https://wordpress.org/support/users/jorisvst/)
 * Last activity: [1 year ago](https://wordpress.org/support/topic/invalid-sql-report/#post-18476831)
 * Status: resolved