PHP/MySQL Full Database Search and Replace

by irms

I wrote this because I often need to move data-driven sites from development to production and addresses change.  (WordPress sites, WordPress MUDrupal,  and so on.)  This means I have to go through and change every occurrence of the url as it appears in the database.  So when the first few solutions for changing the site url aren’t doing the trick, I have to resort to the following MySQL snippet for updating every single table.

To find a string in a certain field and replace it with another string:

update [table_name]
  set [field_name] = replace([field_name],'[string_to_find]','[string_to_replace]');
Dangerous Code Ahead

Well, I got tired of doing that and wrote a handy snippet to find and replace across an entire database.

There are no checks or safeguards!  This is a quick and dirty script to be used at your own risk!

But if it’s useful, please feel free to say so in the comments.


UPDATE

Added two checkboxes:

  1. Set the script to never timeout (use with caution!)
  2. Do a case-insensitive find and replace. (use with caution!)

Download the updated .rar here

UPDATE 2

I just found a much better script than mine for moving WordPress installs, check it out here, and make sure you let them know that it was useful to you:

http://spectacu.la/search-and-replace-for-wordpress-databases/