WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. w3developing
    Member
    Posted 1 year ago #

    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 EXISTSwp_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
        )
  2. s_ha_dum
    Member
    Posted 1 year ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic