Skip to:
Content
Pages
Categories
Search
Top
Bottom

Query works in phpMyAdmin but not through BP


  • panosa1973
    Participant

    @panosa1973

    Hi all,
    I use the below code to modify the member directory and only return a user’s matches. For example, if I’m a man looking for a Woman the directory will default to only showing all Women who are looking for a Man.

    function modify_directory ($query_string, $object) {
    	global $wpdb;
    	if ($object != 'members')  return $query_string;
    	$my_user_id = bp_loggedin_user_id();
    	$my_gender = $wpdb->get_var("SELECT value FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 193 AND user_id = $my_user_id");
    	$i_am_looking_for = $wpdb->get_var("SELECT value FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 197 AND user_id = $my_user_id");
    	
    	//Get all users that are what the logged_in member is looking for THIS RETURNS NOTHING
    	 $my_matches = array($wpdb->get_col("SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 193 AND value = '$i_am_looking_for' AND user_id IN (SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 197 AND value = '$my_gender')"));
    
    	$include = implode(",", $my_matches);
            if (!empty ($query_string))  $query_string .= '&';
    	$query_string .= 'include='. $include;
    	return $query_string;
    }
    add_filter ('bp_ajax_querystring', 'modify_directory', 20, 2);

    The problem is that $my_matches returns nothing. i.e. echo $my_matches[0] outputs Array.

    When I run Select user_id from wp_bp_xprofile_data where field_id = 197 and value = ‘Man’ and user_id in (select user_id from wp_bp_xprofile_data where field_id = 193 and value = ‘Woman’)
    in phpMyAdmin it works, but when this runs in here it doesn’t work.

    Another note: This: echo "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 193 AND value = '$i_am_looking_for' AND user_id IN (SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 197 AND value = '$my_gender')" . ' -- $matches[0]= '. $matches[0];
    returns this: SELECT user_id FROM wp_bp_xprofile_data WHERE field_id = 193 AND value = 'Woman' AND user_id IN (SELECT user_id FROM wp_bp_xprofile_data WHERE field_id = 197 AND value = 'Man') -- $my_matches[0]= Array

    Any ideas?
    Thank you in advance.
    -Panos

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

  • shanebp
    Moderator

    @shanebp

    $wpdb->get_col returns an array.
    Why are you setting the results to an array?
    $my_matches = array($wpdb->get_col ...

    >The problem is that $my_matches returns nothing. i.e. echo $my_matches[0] outputs Array.
    An array is being returned, that is different than returning ‘nothing’.
    Use var_dump( $my_matches ) to examine both the structure and the contents of the results.


    panosa1973
    Participant

    @panosa1973

    Thanks @shanebp. I figured out what was going on. $my_matches = array($wpdb->get_col ... was producing a two-dimensional array, that’s why $my_matches[0] was not returning a value. The $user_ids ware actually being stored in $my_matches[0][0] ($my_matches[0][1], $my_matches[0][2] and so forth), which is unnecessary. So I changed this, $my_matches = array($wpdb->get_col ... to this, $my_matches = $wpdb->get_col ... and now $my_matches[0] ($my_matches[1], $my_matches[2] etc.) returns the actual $user_id and the pagination works as it should.

    The only thing is that the All Members box still displays the total member count but I don’t really care because I don’t want it there anyway so I hid it with CSS.

    Thank you!
    -Panos

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Skip to toolbar