Title: MySQL Query w/ WordPress $wpdb will not return AND statement array() empty
Last modified: August 20, 2016

---

# MySQL Query w/ WordPress $wpdb will not return AND statement array() empty

 *  [w3developing](https://wordpress.org/support/users/w3developing/)
 * (@w3developing)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/mysql-query-w-wordpress-wpdb-will-not-return-and-statement-array-empty/)
 * Query is working, but when I try to add an AND statement, my results return empty.
   I’ve tried many data fields and various options of content. Ensure there was 
   matching data and so forth. I tried other fields in the table as well as other
   field data types. Leads me to believe there is something wrong with my query 
   format, but I cannot get it kicking.
 * **WordPress 3.4.1**
 * Table Structure — — Table structure for table wp_postmeta —
 * `CREATE TABLE IF NOT EXISTS`wp_postmeta` (
    `meta_id` bigint(20) unsigned NOT
   NULL AUTO_INCREMENT, `post_id` bigint(20) unsigned NOT NULL DEFAULT ‘0’, `meta_key`
   varchar(255) DEFAULT NULL, `meta_value` longtext, PRIMARY KEY (`meta_id`), KEY`
   post_id` (`post_id`), KEY `meta_key` (`meta_key`) ) ENGINE=InnoDB DEFAULT CHARSET
   =utf8 AUTO_INCREMENT=416 ;`
 * **Working example: (1)**
 *  `$querystr = “
    SELECT * FROM $wpdb->posts wposts LEFT OUTER JOIN $wpdb->postmeta
   wpostmeta ON wposts.ID = wpostmeta.post_id WHERE wpostmeta.meta_key = ‘_wp_attached_file'”;
   And statement example: AND wpostmeta.meta_value LIKE ‘%on%’`
 * **No return example: (2)**
 *  ` $querystr = “
    SELECT * FROM $wpdb->posts wposts LEFT OUTER JOIN $wpdb->postmeta
   wpostmeta ON wposts.ID = wpostmeta.post_id WHERE wpostmeta.meta_key = ‘_wp_attached_file’
   AND wpostmeta.meta_value LIKE ‘%on%'”; This is what the return results look like
   from query (1) above:`
 *     ```
       [0] => stdClass Object
           (
               [ID] => 7
               [post_author] => 1
               [post_date] => 2012-08-15 04:49:33
               [post_date_gmt] => 2012-08-15 04:49:33
               [post_content] =>
               [post_title] => 1203479739_pmt
               [post_excerpt] =>
               [post_status] => inherit
               [comment_status] => open
               [ping_status] => open
               [post_password] =>
               [post_name] => 1203479739_pmt
               [to_ping] =>
               [pinged] =>
               [post_modified] => 2012-08-15 04:49:33
               [post_modified_gmt] => 2012-08-15 04:49:33
               [post_content_filtered] =>
               [post_parent] => 6
               [guid] => http://localhost/test.org/2012/wp-content/uploads/2012/08/1203479739_pmt.jpg
               [menu_order] => 0
               [post_type] => attachment
               [post_mime_type] => image/jpeg
               [comment_count] => 0
               [meta_id] => 6
               [post_id] => 7
               [meta_key] => _wp_attached_file
               [meta_value] => 2012/08/1203479739_pmt.jpg
           )
       ```
   

Viewing 1 replies (of 1 total)

 *  [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * (@apljdi)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/mysql-query-w-wordpress-wpdb-will-not-return-and-statement-array-empty/#post-2993380)
 * Are you running it through `$wpdb->prepare`? Because if you are running it through`
   $wpdb->prepare` you will have to double those percent signs.
 * [http://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks](http://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks)

Viewing 1 replies (of 1 total)

The topic ‘MySQL Query w/ WordPress $wpdb will not return AND statement array() 
empty’ is closed to new replies.

## Tags

 * [join](https://wordpress.org/support/topic-tag/join/)
 * [MySQL](https://wordpress.org/support/topic-tag/mysql/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [select](https://wordpress.org/support/topic-tag/select/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)
 * [wpdb](https://wordpress.org/support/topic-tag/wpdb/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [s_ha_dum](https://wordpress.org/support/users/apljdi/)
 * Last activity: [13 years, 9 months ago](https://wordpress.org/support/topic/mysql-query-w-wordpress-wpdb-will-not-return-and-statement-array-empty/#post-2993380)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
