Skip to:
Content
Pages
Categories
Search
Top
Bottom

Some questions about BuddyPress friendships

  • Hey, I’m new here and also new with BuddyPress.
    I have installed BP and I’m now trying to add some functionality within the code of my website. Right now I’m working with the friendship requests. I have a button that is showed if two persons are not friends, and when clicked, it calls (via AJAX) the BP function friends_add_friend which takes as parameter the two IDs.
    First question of all: this is unbelievably slow. I use to alert the answer of my AJAX call, and that takes forever. Is this normal?
    Anyhow the very important question is the following: why when I call friends_check_friendship_status it always returns me “not_friends” even though I actually called the function friends_add_friend? The funny thing is that in my DB in the table wp_bd_friends I have the correct IDs shown in the entry and 0 in “confirmed” which means that is a friendship waiting for approval (pending). This means that even though the function call was slow it worked! Yay! But still, the second function call always returns “not_friends” when it should return “pending”.
    Any comments, thoughts, tips?

    Edit:
    I’ve just tried by manually accepting the friend request (putting 1 in the column “is_confirmed”) but still the function friends_check_friendship_status returns “not_friends”. No idea what to do.

Viewing 22 replies - 1 through 22 (of 22 total)
  • Bump. Sorry don’t want to be annoying, but it’s pretty important and I’m in a hurry.

    I’ve just realized I have a couple of warnings from WordPress, don’t know if this can be helpful to help me out:

    Warning: mysql_real_escape_string(): 11 is not a valid MySQL-Link resource in /path/to/wp-includes/wp-db.php on line 785
    Warning: mysql_error(): 11 is not a valid MySQL-Link resource in /path/to/wp-includes/wp-db.php on line 1119

    These happens only when calling the above stated functions of BuddyPress.
    Thanks!

    BuddyPress’ SQL is fine. Your SQL server might be dropping connections. Is this on your own server, or is it being hosted by someone? I’d suggest that you ask them to check your SQL database and look at the number of active connections allowed. Something must be a little bit off-kilter somewhere, or they are throttling database queries.

    Thanks a lot for the reply!
    It’s not indeed my own server, I’m working on someone else’s. What should exactly ask them? To allow a greater number of active connections?
    Thanks!

    Ask them if they see anything relevant in the log file or from any usage stats they might record.

    Ok I just sent them an e-mail, thanks.
    What could be the “relevant things” in the log file that may arise? Do you think that they are “lowering” the number of allowed queries per second and this may be the issue for which the functions are not returning the proper result?

    I don’t think the problem is the number of queries actually…
    I did a print_r over the $wpdb and the last query done after my call to friends_check_friendship_status is
    SELECT id, is_confirmed FROM wp_bp_friends WHERE (initiator_user_id = 0 AND friend_user_id = 0) OR (initiator_user_id = 0 AND friend_user_id = 0)
    Which is pretty strange since I am SURE 100% I pass some real ids to the function.
    Any ideas?

    Avatar of Boone Gorges
    Boone Gorges
    Keymaster

    @boonebgorges

    If the last query reads 0s where there should be valid ids, you are probably not constructing the query correctly. Are you using $wpdb->prepare, with sprintf syntax?
    `$query = $wpdb->prepare( ‘SELECT id, is_confirmed FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d AND friend_user_id = %d) OR (initiator_user_id = %d AND friend_user_id = %d)’, $initiator_user_id, $friend_user_id, $initiator_user_id, $friend_user_id );`

    (I’m not sure if this is your exact query, but you get the idea.)

    If it still isn’t working, then as an experiment, try passing the prepare() arguments as strings – replace %d with %s in the replacement patterns.

    I suggest you dump your variables/query string just before AND just after the query is constructed in this manner, to see whether prepare() is causing the problem.

    I’ve done all the inspection I could, I discovered that
    1) the prepare function when doing func_get_args (or whatever is the name) it gets NOTHING as result, so the query is indeed with ids 0s. Didn’t do what you said, Boone Gorges, even though I really thank you for your concern, I think I would have arrived to the same result :).
    2) if I avoid calling prepare and feed the query directly to $wpdb->get_results (I’m always in check_is_friend function of buddypress) when it does $this->result = @mysql_query( $query, $dbh ); the result is NOTHING. No idea why, because $query just before this function call is the correct query with the correct IDs.

    Any ideas?

    Avatar of Boone Gorges
    Boone Gorges
    Keymaster

    @boonebgorges

    I realize that the query is with 0s. That is the problem. I am suggesting that even though you are “sure 100%” that you are passing the correct IDs to the function, the way that you are concatenating the query string might be corrupting it.

    In any case, I am confused about what’s actually failing here. Are you using the function BP_Friends_Friendship::check_is_friend(), or are you writing your own function? If the former, change the `$result = ` line to the following
    `$query = $wpdb->prepare( “SELECT id, is_confirmed FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d AND friend_user_id = %d) OR (initiator_user_id = %d AND friend_user_id = %d)”, $loggedin_userid, $possible_friend_userid, $possible_friend_userid, $loggedin_userid );
    print_r( $query );
    $result = $wpdb->get_results( $query );`
    so that you can see if the query is being put together correctly. If not, dump the contents of $loggedin_userid and $possible_friend_userid just before these lines to see if you are, in fact, passing values to the function.

    I’m using the BP function.
    It is the former indeed, and I managed to trace the input down to $wpdb->query (if avoid using prepare) or to prepare, to see that it doesn’t work (with prepare the $args = func_get_args() gets nothing as parameter even though I checked they are correct the second is wrong in that line I posted above).

    Anyhow here is the result:
    SELECT id, is_confirmed FROM wp_bp_friends WHERE (initiator_user_id = 0 AND friend_user_id = 0) OR (initiator_user_id = 0 AND friend_user_id = 0)
    This is what I get printed out. If I inspect the values $loggedin_userid, $possible_friend_userid, $possible_friend_userid, $loggedin_userid they have the correct values.
    In prepare the problem is this line “$args = func_get_args();” that gets wrongly the parameters (inspecting args returns nothing).

    Avatar of Boone Gorges
    Boone Gorges
    Keymaster

    @boonebgorges

    I doubt it’s a problem with prepare() itself, since that method is used on nearly every query in BuddyPress and I have never heard of this problem before.

    It could be the case that $loggedin_userid and $possible_friend_userid are improperly cast. Try switching %d with %s in check_is_friend() and see what happens.

    Again I used your code and the result is:
    SELECT id, is_confirmed FROM wp_bp_friends WHERE (initiator_user_id = ” AND friend_user_id = ”) OR (initiator_user_id = ” AND friend_user_id = ”)
    Which is normal I guess, because the passed variables are integers, right?

    Anyhow since I set manually the friendship I tried to call another function (the one that counts the number of friends you have) and also that one returns 0. This is truly unbelievable.

    Bump! It’s really of extreme importance.
    Moreover I figured out also that messages don’t work either: actually if I try to call a function defined in bp-messages-classes.php it says it doesn’t exist… while for friends the methods are found by the compiler this doesn’t happen for the messages… I’ve checked on the admin board and the messages are enabled (I also have the tables in my database to handle messages).

    May the problem with friends be related with this one?

    I’ve reinstalled BP and WP… the error remains but now it does find methods for sending messages.
    There is a bug in the get_inbox_count method if you want to know.

    ( !$unread_counts = $wpdb->get_results($sql) )
    return false;

    This line in my case had an error, even though the result existed, it entered inside the if and returned nothing as result, while the query existed, the get_results returns an (empty) array. I commented it out and now it works.

    But the friends error remains as is. Any ideas?

    I’ve discovered that if I access the page outside the scope of buddypress and wordpress (that is, accessing to the direct link without passing by my website) the thing returns a correct result for the friends (if I hardcode the input of course).
    What could this mean? I require_once in my index wp-config.php and also in my friend_page (which is loaded on top of the index, in a slider that comes down). Is there a problem with this approach?

    I’ve rewritten all the code for friends and messages myself. Thanks anyway for the support.

    Avatar of Boone Gorges
    Boone Gorges
    Keymaster

    @boonebgorges

    Sorry we couldn’t be of more help. I am really perplexed about your situation. There must be a conflict with something else in your setup, as I have never heard of this kind of problem before. If you ever feel like sharing the code you wrote to replace it, feel free.

    Sadly it’s not something I can decide to share, it’s a work and I don’t really feel like asking this thing.
    Anyhow the problem I believe was that if I accessed the variables from index everything was fine, friends were found, etc. The problem is that by the means of a javascript slider I use to import pages that then executed the code… maybe this was puzzling some of the very basic variables of wordpress and returned bad results.

    What can I suggest to you as developers it to try to implement a custom template (even a simple page that uses the wordpress variables) and on top of that implement some javascript that loads another php page on top of the index (no page reloading) that uses again the wordpress variables… this I think would arise the error again. I think this is a major bug and should be documented / resolved, but it’s up to you ;).
    Thanks again for the support. I really appreciated that.

    Avatar of Boone Gorges
    Boone Gorges
    Keymaster

    @boonebgorges

    Ah, yeah, if you are trying to access things in an ajaxy way, and you’re not hooking into WP in the right way, you might hit some problems because BP and other plugins might not be fully loaded. Were you using a wp_ajax_ hook? Generally speaking, you can’t simply include BP files manually and have things work properly – all of WP needs to be loaded. http://codex.wordpress.org/AJAX_in_Plugins#Ajax_on_the_Viewer-Facing_Side

    I’m not sure I entirely understand your suggestion about an additional template. If you have even a rough sense of what such a thing might look like, I invite you to work up a patch (even a super sketchy one) and post it as an enhancement ticket on trac.buddypress.org (with your buddypress.org user/pass).

    Nothing like wp_ajax_hook on my code… the code just loaded pages with javascript, no ajax was used there, ajax was just some side cool think like “send a message without refreshing” and super-easy things like that.
    Anyhow my suggestion was just to try and watch what happens on that sort of things, I wasn’t actually asking for a new template, that’s just how I have things going and how the problem arose. :)

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

You must be logged in to reply to this topic.