Object of class wpdb could not be converted to string.. (3 posts)

  1. i_banks
    Posted 4 years ago #

    I'm trying to change the url in various rows in multiple wp databases. So I wrote a piece of code that I think should work, but I'm getting an error with the global variable $wpdb.

    The error states:
    Catchable fatal error: Object of class wpdb could not be converted to string in /home/isaiahb/public_html/sexyafter30/wp-content/themes/twentyten/functions.php on line 107

    Here's a snippet of the code:

    function updateDBUrls(){
    global $wpdb;
    $my_dbs = array("bb_meta" => "meta_value", "bp_activity" => array("action", "content", "primary_link"), "posts" => "guid", "users" => "user_url");
    foreach($my_dbs as $key => $value){
    	$rows = $wpdb->get_row("SELECT * FROM $wpdb->".$key." WHERE ".$value." REGEXP ' (.*)http://sociallyaffluent.com/21andolder(.*) ' ", ARRAY_A);
    	foreach ($rows as $singlerow){
    		$new_url = changeUrl($singlerow[$value]);
    		$wpdb->query(" UPDATE $wpdb->".$key." SET ".$value." = ".$new_url." WHERE ".$value." REGEXP ' (.*)http://sociallyaffluent.com/21andolder(.*) '");
    } //end of inner foreach loop
    } // end of beginning foreach loop
    }// end function updateDBUrls

    Apparently, this is the problem line (107):

    $rows = $wpdb->get_row("SELECT * FROM $wpdb->".$key." WHERE ".$value." REGEXP ' (.*)http://sociallyaffluent.com/21andolder(.*) ' ", ARRAY_A);

    Can anyone see where or if I'm misusing the $wpdb global variable?

  2. Chris Berthe
    Posted 3 years ago #

    Did you happen to figure this out? I'm doing a very similar thing in one of my functions and receive this same error.

    Edit: Clearly the $wpdb-> " . $key that's having an issue. Figured it out for myself by removing $wpdb.

  3. Mark / t31os
    Posted 3 years ago #

    Yep, that'll be your problem area, when you reference an object property inside a double quoted string the variable needs to wrapped in curleys or concatenated..

    $rows = $wpdb->get_row("SELECT * FROM {$wpdb->$key} WHERE $value REGEXP ' (.*)http://sociallyaffluent.com/21andolder(.*) ' ", ARRAY_A);


    $rows = $wpdb->get_row("SELECT * FROM ".$wpdb->$key." WHERE $value REGEXP ' (.*)http://sociallyaffluent.com/21andolder(.*) ' ", ARRAY_A);

    This part..


    ..gets treated literally when seperated from the object property..

    Not quite sure i'm explaining, please just take my word for it and use on the above approaches.. ;)

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.