How to do a mass text search and replace with MySQL

October
24

I noticed that all my images on the posts here weren’t showing and it was because the url to their location was still pointing at the sub blog on The Comluv Network so I needed a way to edit the database to update the url to point to the files held here.

Easy with a REPLACE function on the wp_posts table.

You just need 3 things for the replace command :

  1. The row you’re altering
  2. The string you’re searching for
  3. The string you’re replacing with

UPDATE `wp_posts` SET `post_content` = REPLACE( `post_content` , "fiddyp.comluv.com/files/", "fiddyp.co.uk/wp-content/uploads/" )

It did the whole table of posts in less than a second. Sweet, now all my images are back to normal on this site.

Code, PHP


4 Comments zu “How to do a mass text search and replace with MySQL”

  1. witchypoo
    24.10.09 3:05 pm

    Twitter: psychicgeek
    I’ll be bookmarking this, thanks!
    witchypoo´s last blog ..Bust A Move My ComLuv Profile

    #1

  2. Kalvster
    24.10.09 10:30 pm

    Very useful, its in my bookmarks now! By the way, nicely designed website.
    Kalvster´s last blog ..Free Ubuntu VPN with HotSpot Shield (My Attempt) My ComLuv Profile

    #2

  3. Dennis Edell
    26.10.09 5:17 pm

    Hey buddy, sorry this is so OT. My apologies for being gone so long but my current post describes it a bit.

    I’m ready now to re-subscribe and get back into your awesome blog! Slowly at first until I get full swing again. :)
    Dennis Edell´s last blog ..What I’m Doing – What You Should Expect In The Very Near Future. :) My ComLuv Profile

    #3

  4. Dennis Edell
    26.10.09 5:18 pm

    Did you remove subscribe to comments, or is it automatic now?
    Dennis Edell´s last blog ..What I’m Doing – What You Should Expect In The Very Near Future. :) My ComLuv Profile

    #4

Your Comment

CommentLuv Enabled