• 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 ' (.*)https://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 ' (.*)https://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 ' (.*)https://sociallyaffluent.com/21andolder(.*) ' ", ARRAY_A);

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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.

    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 ' (.*)https://sociallyaffluent.com/21andolder(.*) ' ", ARRAY_A);

    or

    $rows = $wpdb->get_row("SELECT * FROM ".$wpdb->$key." WHERE $value REGEXP ' (.*)https://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.. ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Object of class wpdb could not be converted to string..’ is closed to new replies.