Your code would work, yet I believe that as the website grows heavier, such an algorithm will need optimization. Think big numbers, like a hundred thousand DB entries, 10 million viewers/month, 200 users launching a search at the same time... Think would get messy.
This is exactly one of these cases where you can benefit from Open Source projects. People somewhere in the world have spent years optimizing their code so that they could deliver the best product, and they're giving it away for free. You could for example take a look at the excellent
Sphinx project.
Remember, as a web developer your job is to deliver a website to the client as fast and as cheap as possible. Benefiting from open source products can often give you a great edge.
Unless you're trying to implement the search function as an exercise, in which case it's a whole other problem.