Saturday, October 17, 2009

PHP MySQL Single Field Search Script

I wanted to provide a way for users to search a limited access archival digital collection of over 36,000 records, which contained meta data and where approx 60% of the collection had machine generated OCR data.

To solve this I decided to batch load the “searchable” meta data and OCR data to a three field MySQL database and make this searchable via a PHP script. The database contained an index field, a title field and search field. The index and title fields are largely self explanatory with the index field containing a value that would allow a URL to the digital object concerned to be generated. However the search field contained a concatenation of all the relevant searchable meta data fields and then appended the machine generated OCR data, if it was available.

I then constructed a short PHP script to search the search field and return the results. I was surprised at how efficiently this process worked for our needs and decided that the basic code to search a single field MySQL database might be of value to others! Copy the code for achieving this here. If you save this file as search.php and then edit it in the appropriately commented locations, you should quite quickly get it working. (This code was actually working on my test server prior to making it available)

There is no attempt in my PHP code to evaluate the relevance of the results. They are just displayed sorted on the search field. I have since successfully used this PHP search script as the bases of more complex search scripts on other projects.