WordPress.org

Support

Support » How-To and Troubleshooting » wpdb->get_results only works in some themes

wpdb->get_results only works in some themes

  • Hey, I’m updating my blog from an older version of wordpress, and our comments section stopped functioning after updating.

    I think I’ve traced down the problem to comment-template.php, where the following code is run:

    if ( $user_ID) {
    		$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) )  ORDER BY comment_date_gmt", $post->ID, $user_ID));
    	} else if ( empty($comment_author) ) {
    		$comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC') );
    	} else {
    		$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND ( comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) ORDER BY comment_date_gmt", $post->ID, wp_specialchars_decode($comment_author,ENT_QUOTES), $comment_author_email));
    	}

    For some reason, this code returns $comments as an empty array in my theme, and returns an array of length 3 in the official wordpress theme TwentyEleven, even though they receive the exact same query text (it enters the first if-block).

    Do I need to initialize $wpdb somewhere? Is there some variable or function that needs to be called or set before I get to this call in the code?

    Thanks for any help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • To clarify, get_results is called on this line:

    $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) ) ORDER BY comment_date_gmt", $post->ID, $user_ID));

    I have tried to print the queried text to the website so that I can control that the query string is identical for both themes, and they are.

    Yet for some reason only the official wordpress theme gives more than one matching result. I can’t find any relevant documentation in the wordpress docs.

    Ok, I’ve started digging in the wordpress code a bit more deeply. It turns out that the prepare method in wp-db.php was deleting the 1s from my query. Why this didn’t happen in the official wordpress theme I don’t know, but when I disabled it things got a bit better and a bit more obtuse.

    I have now dug through the query function in wp-db.php and verified that the exact same query is run in both themes, yet it doesn’t correctly return in my theme. It brings back a row containing a user name and no actual post. Then it gets to this piece of code:

    $num_rows = 0;
    			while ( $row = @mysql_fetch_object( $this->result ) ) {
    				$this->last_result[$num_rows] = $row;
    				$num_rows++;
    			}

    This code checks through every row that was returned, but the result in my theme is that doesn’t have a complete post. Like I said, this is despite the query function receiving the exact same $query as it does in the official theme.

    The query is correctly formatted and has no syntax errors. I have verified that such a query, if run against the database, should return the comment I want displayed. There are no connection errors.

    Any wordpress gurus want to tell me what’s going on here?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wpdb->get_results only works in some themes’ is closed to new replies.