1

I need to replace an html code in multiple posts. This is the code I am using:

UPDATE wp_posts SET post_content = REPLACE(post_content, 'target="_top" class="text"') WHERE 'post_content'  = 'class="text"'

But I am getting an error:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') WHERE 'post_content' = 'class="text"'' at line 1

Is there any solution to make it work properly ?

1
  • how are you executing the sql? Commented Aug 31, 2012 at 19:06

3 Answers 3

3

REPLACE takes three arguments, you only gave two. You didn't tell it what to replace the matched text with.

Sign up to request clarification or add additional context in comments.

Comments

1

You need three arguments for replace, and you will also need to use LIKE in your WHERE clause or you won't get any matches. Finally you don't want single quotes around post_content in the WHERE clause. Either use backticks or nothing.

UPDATE wp_posts
SET post_content = REPLACE(post_content, 'class="text"', 'target="_top" class="text"')
WHERE post_content LIKE '%class="text"%'

Comments

1

Take a look at the manual for proper use of REPLACE. You need to provide 3 arguments:

REPLACE(str, from_str, to_str)

Returns the string str with all occurrences of the string from_str replaced by the string to_str. REPLACE() performs a case-sensitive match when searching for from_str.

mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
        -> 'WwWwWw.mysql.com'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.