Untested code with mysql, should work though might need to be tweaked a bit