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.

Friday, September 18, 2009

Automate Equipment Inventory to Database

It's been a while since I have posted to this blog. Mostly it has been because we had some major staff movements that have taken some time to work through to the point where making some space to write here is now somewhat realistic!

I want to share an improvement to a script I blogged in Feb 2007 which was about using a Windows Powershell script to query the WMI Win32_Product so as to retrieve information about all the software on a computer.

The original script output to a HTML file and has served us well, but I have occasionally thought that it would be great to also be able to write some or all of the data to a MySQL database. Recently I played around with this idea and managed to achieve a reasonable result.

I decided to use the .NET assembly available for this purpose from the MySQL site. The reason I decided to use the .NET assembly was that although the assembly can be installed using the installer the reality for distribution purposes was the the installer in fact copies in and then registers a .DLL file which if you want, can just be directly copied onto the target systems. Providing you specify the exact location of this file in the Powershell script, the fact that it is not "registered" does not matter!


  1. As the script writes output to a MySQL server located on another machine. You can get the setup for the MySQL server from my personal website.
  2. You can get a copy of the GetInventory_DB.ps1 Powershell script and copy this script to the desired location on the machine(s) you wish to get inventory data from.
  3. Also you will need to either install the MySQL .NET connector or download it and just copy in the .NET assembly .DLL file called "MySql.Data.dll" to the desired location on the machine(s) you wish to get inventory data from.

Note: If you use different file locations for these files than what the script uses, you will need to edit the script accordingly. Also you will need to edit the location of the MySQL server...

We schedule the script to run from the AT command once a day on the local machines when the workstations are not in use. This means that the data is up to date with any changes made in the previous 24 hours. Intially we are accessing the MySQL database using an ODBC connector and a MS Access Database. I have since written a PHP script that interacts with the MySQL database. Rename the script "search.php" and edit it in the places commented so as to reflect your database, username and password and it should work! (This script was copied directly from my test server...).