Database error issue and solution
-
For the past few months we noticed that we were getting a lot of the following error in our WordPress log file.
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 ‘AND wp_posts.post_status = ‘publish’ AND wp_posts.post_type = ‘post’ ORDER BY co’ at line 1 for query SELECT wp_posts.post_title, wp_posts.ID, wp_posts.post_content, wp_posts.comment_count FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) WHERE term_id= AND wp_posts.post_status = ‘publish’ AND wp_posts.post_type = ‘post’ ORDER BY comment_count desc LIMIT 5 made by require(‘wp-blog-header.php’), require_once(‘wp-includes/template-loader.php’), include(‘/themes/fonolo/single.php’), get_sidebar, locate_template, load_template, require_once(‘/themes/fonolo/sidebar.php’), dynamic_sidebar, call_user_func_array, WP_Widget->display_callback, WpPopularPostsTool->widget, do_action(‘TB_RenderWidget’), call_user_func_array, WpPopularPostsTool::render, WpPopularPostsTool::ti_popular_posts
As you can see from above, “
WHERE term_id= AND” makes it a malformed query.We traced the problem’s origins to
if($my_id==0): if(is_category()): $my_title = strtolower(single_cat_title('', false)); $my_id = get_cat_ID($my_title); elseif(is_tag()): $my_title = $my_id = intval(get_query_var('tag_id')); elseif(is_single()): $my_title = get_the_category(); $my_id = $my_title[0]->cat_ID; endif; endif;Under certain circumstances, $my_id will becomes null at “
$my_id = $my_title[0]->cat_ID“, which causes the malformed query.This can be remedied several ways, one of which is to force integer casting:
$my_id = intval($my_title[0]->cat_ID);What we did was to implement a more blanket solution after the if statement:
if($my_id==0): if(is_category()): $my_title = strtolower(single_cat_title('', false)); $my_id = get_cat_ID($my_title); elseif(is_tag()): $my_title = $my_id = intval(get_query_var('tag_id')); elseif(is_single()): $my_title = get_the_category(); $my_id = $my_title[0]->cat_ID; endif; endif; if(empty($my_id)): $my_id = 0; endif;Could future versions include this or a solution that makes sure $my_id can’t become null?
Thanks,
Steven
The topic ‘Database error issue and solution’ is closed to new replies.