mySQL Database Search & Replace With Serialized PHP [Updated]

Ever needed to migrate a database to a new server or website (especially with WordPress and other PHP applications) and been stuck because when you do a search and replace some of the data seems to get corrupted?

Please note that a newer version of this code is now available from my Interconnect’s site over at https://interconnectit.com/search-and-replace-for-wordpress-databases/ 

Ever needed to migrate a database to a new server or website (especially with WordPress and other PHP applications) and been stuck because when you do a search and replace some of the data seems to get corrupted?

Serialized PHP Arrays Cause Problems

In PHP one of the easiest ways of storing an array in a database is to use the serialize function.  Works a treat, but the downside is that you’re not storing data with a cross platform method.  In many product development environments this would get you a stern talking to, but in the world of web development where deadlines are tight and betas are the norm, this seems to be overlooked somewhat.

So what we have are tables full of data that can’t be easily edited by hand.  For example:

;a:3:{s:5:"title";s:17:"This Week\'s Poll";s:18:"poll_multiplepolls";s:0:"";s:14:"multiple_polls";N;}

Say you had thousands of records like the one above, and the word ‘multiple’ needs to be changed to ‘happy’.  Two bits would change – poll_multiplepolls would now read poll_happypolls and multiple_polls would read happy_polls.  In both cases you would have three characters fewer to deal with.

Fine, you may think, but you can only do the change by hand because where it says s:18:"poll_multiplepolls" it now has to say s:15:"poll_happypolls" – see the difference?  S18 spells out the length of the following string, and it has to be changed to s:15

I’ll say right now, that that was a pain.  For simple arrays I wrote the straightforward PHP Serialization fixer code, which got me out of many a pickle – do the search and replace without worrying, and then run the script.  Fixed about 90% of problems.

Multidimensional Array Problem

Sadly those 10% of problems left were a real pain.  I needed something more robust.  Something more powerful.  And finally today it was a Bank Holiday in the UK – that means no phone calls… I could have a quiet day of coding and concentrate on the best solution to this problem.

What I’ve done is to write a database search and replace utility in PHP that scans through an entire database (so use with care!) which is designed for developers to use on database migrations.  It’s definitely not what you’d call an end-user tool, though I may sanitize it at some point and turn it into an easy to use WordPress plugin.  Thing is – this is dangerous code – sometimes I think it’s better to make it deliberately a bit tricky, don’t you?

It’s not that bad though – if you can manually install WordPress, you can easily configure the database connection settings.

What the code does is to look at the database, analyse the tables, columns and keys, and then starts reading through it.  It will attempt to unserialize any data it finds, and if it succeeds it will modify that data then reserialize it and pop it back in the database where it found it.  If it finds unserialized data it will still carry out the search and replace.

Use in WordPress

In most WordPress migrations you tend to have the primary problem of changing the domain name entries in content, settings and widgets – you simply need to put in the $search_for string the old domain address (including the http if it’s there) as seen on the database, and the new one into $replace_with.  Then put this script onto your server, and run it by visiting it in your browser or inputting the appropriate command line – depending on your server configuration.

Other things you may want to check are for plugins or themes that have made the mistake of storing the full server path to the installation – cFormsII does this, for example.  You will need to find out your old and new server paths and use those, in full, for another iteration of this script.

After less than a second of running, you should have a freshly edited database.  It may take a little longer on slow or share hosting, or if you have a very large database, but on my laptop I can manage around 60,000 items of data per second.

I’ve just used the script to migrate, in its entirety, with content, settings, 87 widgets (yes, really!) and hundreds of images to my localhost server.  It took moments, and the site is perfectly preserved.

Search and Replace Database download.
download file

Search and Replace Database download

BIG WARNING: I take no responsibility for what this code does to your data. Use it at your own risk. Test it. Be careful. OK? Here in the North we might describe the code as being as “Rough as a badger’s arse.” Never felt a badger’s arse, but I’ll take their word for it.

Spectacu.la, the New Design, and Why I’ve Been So Quiet…

Some of you may have noticed that this blog hasn’t been updated properly of late. Not much fresh content, no motorsport reports… in fact, not much at all. Here’s why…

My site's not been quite as desolate this site in Sofia, Bulgaria, but...  (Image by niv, who at 2008-12-01 released it as CC Share Alike @ Flickr)
My site isn't so desolate (nor beautiful) as this one in Sofia, Bulgaria - by niv at Flickr - credit and link at the bottom of this article

Some of you may have noticed that this blog hasn’t been updated properly of late.  Not much fresh content, no motorsport reports… in fact, not much at all.

Well, there are reasons!  First off, work.  And more work.  Basically, early on this year I realised that we simply weren’t making enough to get by, let alone to prosper.  So it became time to actually knuckle down.  I stepped up my networking efforts – getting out there and meeting people, letting them know who we were.  In the end a chance meeting with Matt Wardle, formerly of Black & Ginger, led to us doing several pieces of work.  In a way, that was the start of a path where we learned how marketing and building a small business actually works.

And so it started.  In Spring I built the back-end to an activities site by B&G called Liverpool Active City.  I don’t feel it’s as busy a site as it should be, given the budgets behind it, but our side of it works reliably, so I’m happy.  That was followed by a frantic summer with Politics & The CityJames (my colleague) coding late into the night, and me swearing and cussing over the SUN server that we got lumbered with.  Not being a SUN OS expert I found there were plenty of traps waiting for me.  We went live with hours to spare, and I even managed to arrive at the launch party.  Late and bedraggled, of course, but hey….

We then deliberately avoided taking on large works in order to implement a new WordPress Themes Club.  This club, known as Spectacu.la and with that as its domain name, was a massive project for us and involved lots of tricky work to build a secure, reliable and easy to use platform.  The themes for it needed to be built too.  That was James occupied, basically, from August to the end of November.  In the meantime I did the smaller works (I’m not a web coder by nature – it drives me nuts) and that’s established a rather nice platform for us to hopefully monetise our WordPress skills in a non-consultancy, and therefore highly scalable, way.

And that’s led to us winning a contract with a FTSE250 corporate to replace one of their leading websites with a WordPress based site.  This is a relatively large job for us – but it signifies the scale of interest in the WordPress platform we chose to specialise in.  And you know… it’s going to be brilliant.  We have James, who as a WP coder just rocks, and Mike Little is now working with us too.

And of course, there’s the re-design of this site.  Over the years it’s accumulated a lot of content and I felt it was time to use a theme suited to this – breaking things up into sections and providing easier navigation options.

So, that’s a big part of why I’ve been quiet.  Motorsport has taken a back seat, as have most of my other interests.  The business is now on a far stronger foundation, however, with a good, regular income stream and finally a little bit of money in the bank.  And while travel has taken a real back seat this year, in spring we’re planning a trip to Mexico.  Watch this space!

[Photo credit:  Niv]