in reply to Problem with CGI script

Well, it seems most likely that the open is failing. There are 2 likely scenarios:
  1. $database is not what you think it is. I notice you are setting it on each iteration of the parsing of QUERY_STRING. Is the database guaranteed to be the last element?
  2. The applications cwd is not what you think it is. If you tested using root or your own user-id, and the web server runs the application as a different user, then the working dir may be wrong

fnord