Escaping multiple characters in mySQL query - mysql

I'm trying to figure out how to change a string with another string in mySQL. I don't have much experience with databases. I've read several answers here, but I'm still getting errors.
This is what I started with
UPDATE wp_posts SET post_content = REPLACE (
post_content,
'whatever',
'Hello');
but it keeps giving me errors.
So, I started to escape special characters like ;(),'_> but I still get an error message. What am I missing?
UPDATE wp_posts SET post_content = REPLACE (
post_content,
'whatever',
'<a href="https://www.notarealdomain.com" onClick="ga(''send''\, ''event''\, ''Banner''\, ''Link''\, ''Page''\)\;" target="\\_blank" rel="noopener noreferrer"\>Hello</a\>');

Related

MySQL find and replace a string with variable content

For some reasons, I uninstalled VC Bakery plugin on WordPress, but i have like 6000 posts using VC short codes, I'm trying to replace those shortcodes with something else. For e.g.
What SQL query can I use to turn this content:
[vc_row][vc_column][vc_single_image image="48526" img_size="full"][/vc_column][/vc_row]
To:
<img src="somethinghere" alt=""/>
Applying changes to table "wp_posts", column "post_content".
Any solution ? thanks :)
Why not just use string function replace()?
uppdate wp_posts
set post_content = replace(
post_content,
'[vc_row][vc_column][vc_single_image image="48526" img_size="full"][/vc_column][/vc_row]',
'<img src="somethinghere" alt=""/>'
)
where post_content like '%[vc_row][vc_column][vc_single_image image="48526" img_size="full"][/vc_column][/vc_row]%'

MySQL: Why aren't url's matching when using REPLACE?

My Situation:
I have url's in a field containing blog posts. The url's are being stored in my database with escape characters. My task at the moment is to replace some already inserted 'http' url's with 'https' url's, but REPLACE will match neither the original url nor the escaped url. I can't just replace every instance of 'http:', because I only want to affect certain links in each post, not every link.
I am very familiar with SQL, as well as REPLACE, so I'm not just asking how REPLACE works and how to use it. Another user here has tested my queries in his environment and they work. So, there must be something in my configuration that is preventing the queries from functioning as expected.
I have searched this site and google extensively for several hours and have found nothing specifically addressing my issue. Everything I have tried is included below and if there is something else I should try, I don't know what that is and I haven't found any suggestions/posts/comments that suggest doing anything differently.
Example URL:
http://test01.mysite.com
As Stored in DB:
http:\/\/test01.mysite.com
Code to Re-Create Situation:
DROP TABLE IF EXISTS test_posts;
CREATE TABLE IF NOT EXISTS test_posts (
id int NOT NULL AUTO_INCREMENT,
post_content longtext NOT NULL,
PRIMARY KEY (id)
)
INSERT INTO
test_posts
(post_content)
VALUES
('content content content Link I want to change content content content Link I don\'t want to change content content content Link I want to change content content content Link I don\'t want to change');
If I run
UPDATE
test_posts
SET
post_content = REPLACE(post_content, 'http://test01.mysite.com', 'https://test01.mysite.com');
or
UPDATE
test_posts
SET
post_content = REPLACE(post_content, 'http:\/\/test01.mysite.com', 'https://test01.mysite.com');
zero records are affected.
For testing purposes, I ran the following query which returns 0 rows.
SELECT
*
FROM
test_posts
WHERE
post_content LIKE '%http://test01.mysite.com%'
OR
post_content LIKE '%http:\/\/test01.mysite.com%'
OR
post_content LIKE '%http:\\/\\/test01.mysite.com%'
OR
post_content LIKE 'http:%/%/test01.mysite.com%';
If I run:
SELECT
*
FROM
test_posts
WHERE
post_content LIKE '%http:_/_/test01.mysite.com%'
It does return matches, but that doesn't solve the real problem of how to match when using UPDATE/REPLACE.
I have tried on two different servers and I get the same results on both.
I have tried the following Engine/Collation combinations and all return the same 0 records results:
MyISAM/latin1_swedish_ci
MyISAM/utf8mb4_unicode_ci
InnoDB/latin1_swedish_ci
InnoDB/utf8mb4_unicode_ci
Anybody know how I can write these queries so that REPLACE will find matches to those url's or what settings in my database or PhpMyAdmin may be causing the queries to return/affect 0 rows?
I think the backslash must be escaped in MySQL
field_name LIKE 'http:\\/\\/test01.mysite.com%'
Of course one could go for sure and use the single char wildcard __
field_name LIKE 'http:_/_/test01.mysite.com%'
or for your both cases: an optional backslash:
field_name LIKE 'http:%/%/test01.mysite.com%'
I'm still baffled as to why the queries with LIKE won't work, but, sadly, using those to narrow down the problem clouded my judgement and I didn't try all the same combinations in the REPLACE functions.
The following works:
UPDATE
test_posts
SET
post_content = REPLACE(post_content, 'http:\\/\\/test01.mysite.com', 'https://test01.mysite.com');
If anyone can explain to me why these combinations work with REPLACE, but not with LIKE, I'd really love to know. Thanks!
There is no reason, your query won't work if you have run properly, there is something else, you may be missing here.
UPDATE
test1
SET
name_1 = REPLACE(name_1, 'http:\/\/test01.mysite.com', 'https://test01.mysite.com')
works well and does the job of repalcing the \/ with /.
See screen-shot attached,
You may have some other problem, please check and update the question, if so.
Edit after comments
If you have more data points in URL, change query like below.
UPDATE
test1
SET
name_1 = REPLACE(name_1, '\/', '/')
Above will replace all the occurrence of \/ with /.
As \\ did not work to represent/escape a backslash, use regular expression functions:
REGEXP_LIKE('.*http:\\/\\/test01\.mysite.com.*')
REGEXP_REPLACE(field, 'http:\\/\\/', 'http://')
Here \\ should work.

How to use wildcards on YouTube ID inside MySQL command?

Today I found that my site (WordPress-based) has some mistakes in YouTube urls, like "https://www.youtube.com/embed/VIDEO_ID&autoplay=1". The autoplay should be preceded by ?, so it's returning error.
How can I use mysql to correct it? I know that I can use something like:
UPDATE wp_posts SET post_content =
replace(post_content, '/embed/VIDEO_ID&autoplay', '/embed/VIDEO_ID?autoplay' ) ;
But how to deal with the VIDEO_ID?

Using UPDATE and REPLACE With Wildcards

UPDATE database_posts
SET post_content = REPLACE (post_content,'%submitted by%%%%%', '');
Is it possible in SQL to perform an UPDATE and REPLACE using wildcards? I'm trying to remove the author from the post_content column, everything after/including %submitted by%%%%%.
The submitted by value is always different. I've tried using the query above with no luck.
Thanks!!
If you want to keep everything up to submitted by and nothing after, you can use substring_index():
UPDATE database_posts
SET post_content = SUBSTRING_INDEX(post_content, 'submitted by', 1)
WHERE post_content LIKE '%submitted by%';
I have no idea what all the spaces are between "submitted" and "by", but they are in your question.

Mysql: bulk delete images from posts while still using their thumbnails

I am new in MySQL.
I would like to delete all the images that were uploaded to posts and pages of a Wordpress site, while still using them as attachments/thumbnails (as featured images).
That is why, I can’t bulk delete them from the media library, only from within posts and pages.
Since it’s a huge amount of images, I’d prefer using mysql command.
The string of the images is always written as follows - <img xxxx>, where xxx is varied based on the img location and other details such as alt, width and height etc.
The table is wp_posts and the fieldname is post_content
The closest command I came across to is –
UPDATE wp_posts SET post_content = REPLACE(post_content, '<img%>', '')
WHERE post_content LIKE '<img%>';
It doesn’t work.
Any clues?
I'm not familiar with WordPress database structures, and can't figure out exactly what you mean, but don't you just mean this:
UPDATE wp_posts SET post_content = '' WHERE post_content LIKE '<img%';
This will delete everything from the post_content column that starts with '< img'. Alternatively you can do NULL instead of ''.