I want to find and replace (or more accurately, append) some HTML in one of my MySQL database fields, where the field contains a specific string of text.
I can successfully find the relevant records using this query:
SELECT *
FROM `wp_posts`
WHERE `post_content` LIKE '%some phrase%'
This query returns all the records I want to append HTML to.
So I try the following query to append the HTML I want to use:
SELECT *
FROM `wp_posts`
WHERE `post_content` LIKE '%some phrase%'
UPDATE wp_posts SET post_content = REPLACE(post_content, '</strong></a>', '</strong></a>
<strong>Some additional piece of text</strong></p>')
But an error is returned:
#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 'UPDATE wp_posts SET post_content = INSERT(post_content, '</strong></a>â', '</stro' at line 4
I presume it doesn’t like how I’ve formatted my HTML, but how should it be formatted?
you are trying to execute two different queries. My guess is that you need to execute the following one:
look on UPDATE Syntax , need to be like
Have you tried using the select as an inner query? (assuming
There’s nothing wrong with the content you’re trying to replace. However, the way I see it, you construct your update query the wrong way: You put a
SELECT
right before anUPDATE
query, probably expecting theUPDATE
would only touch theSELECT
ed rows.I think what you want is this:
MySQL >= 5.5 provides XML functions to solve your issue.
You can combine
ExtractValue
,UpdateXML
andREPLACE
functions.Reference: https://dev.mysql.com/doc/refman/5.5/en/xml-functions.html