views:

67

answers:

1

I'm trying to update tables in my wordpress mu database. I want to update all wp_options tables. These tables are named like this:

  • wp_1_options
  • wp_2_options
  • ...and so on.

How do i affect all tables with wp_any-character-here_options? I tried to query for:

UPDATE wp_%_options 
   SET option_value = replace(option_value, 'http://www.old-domain.com', 'http://www.new-domain.com') 
 WHERE option_name = 'home' OR option_name = 'siteurl';

...but mysql complains i have error in syntax...

A: 

You can't wildcard UPDATE statements - you have to write an UPDATE statement for each table.

Untested:

CREATE PROCEDURE cleanup()
BEGIN

  DECLARE i INT DEFAULT 1;

  PREPARE stmt FROM "UPDATE ? 
                        SET option_value = REPLACE(option_value, 'http://www.old-domain.com', 'http://www.new-domain.com') 
                      WHERE option_name IN ('home', 'siteurl')"

  WHILE i <= 10
    EXECUTE stmt USING CONCAT('wp_', i, '_options');

    SET i = i + 1;
  END WHILE;

  DEALLOCATE PREPARE stmt;

END;
OMG Ponies
Oh... really? Is there any other way to automate this or do i really need to change this number 10 times?
Stan
@Stan: You can use MySQL's Prepared Statements to construct the queries dynamically. MySQL does support updating multiple tables, but I don't know if it is possible for your situation.
OMG Ponies
@Stan You should have never created tables like this.
Jonathan Sampson
How in your opinion 'dynamic' version of above query would look like?
Stan
@Jonathan I didn't, it's wordpress mu default, say this to wordpress devs -_-
Stan
What WHILE i <= 10 means? Repeat 10 times?
Stan
@Stan: Yes, `WHILE` will perform what is inside it until `i` is equal to 10.
OMG Ponies