Title: internal server error processing $wpdb query results
Last modified: December 6, 2022

---

# internal server error processing $wpdb query results

 *  [jester48](https://wordpress.org/support/users/jester48/)
 * (@jester48)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/internal-server-error-processing-wpdb-query-results/)
 * I am running the below code and getting an internal error, logs show nothing,
   if i replace `echo $discipline;` with `echo 1;` it works. what am i doing wrong?
   this is for an ajax call back.
 *     ```
       function get_ajax_disciplines(){
       	global $wpdb;
       	$sql = "SELECT DISTINCT meta_value
       			FROM wpau_postmeta wpm
       				INNER JOIN wpau_posts wp
       					ON wp.ID = wpm.post_id
       						AND wp.post_status = 'publish'
       			WHERE wpm.meta_key = 'post_discipline'
       				AND meta_value NOT LIKE 'a:%'
       				AND meta_value NOT LIKE '%|%'
       				AND TRIM(meta_value) != ''
       				AND meta_value IS NOT NULL
       			ORDER BY meta_value ASC";
       	$disciplines = $wpdb->get_results($sql);
   
   
       	foreach( $disciplines as $discipline){
       		echo $discipline;
       	}
       }
       ```
   

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

 *  [Alan Fuller](https://wordpress.org/support/users/alanfuller/)
 * (@alanfuller)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/internal-server-error-processing-wpdb-query-results/#post-16266701)
 * What was the actual error?
 * also try
 * `var_dump($discipline)`
 *  [Alan Fuller](https://wordpress.org/support/users/alanfuller/)
 * (@alanfuller)
 * [3 years, 6 months ago](https://wordpress.org/support/topic/internal-server-error-processing-wpdb-query-results/#post-16266719)
 * I suspect that `$disciplines` is a wp_error object
 * try checking that the error before the loop
 * e.g.
 *     ```
       $disciplines = $wpdb->get_results( $sql );
   
       	if ( is_wp_error( $disciplines ) ) {
       		// do something like log it
       		return;
       	}
   
       	foreach ( $disciplines as $discipline ) {
       		echo $discipline;
       	}
       ```
   

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

The topic ‘internal server error processing $wpdb query results’ is closed to new
replies.

## Tags

 * [wpdb](https://wordpress.org/support/topic-tag/wpdb/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [Alan Fuller](https://wordpress.org/support/users/alanfuller/)
 * Last activity: [3 years, 6 months ago](https://wordpress.org/support/topic/internal-server-error-processing-wpdb-query-results/#post-16266719)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
