Ad Hoc Query tool for SQL


With an SQL Query, there are certain elements (such as which tables are joined to which other tables, and on which fields) which are naturally fixed, and other elements (such as which columns are wanted on the output, and which rows are to be chosen) which may vary from one query to another - especially in an application where a database is being used to store large amounts of data gathered for statistical analysis.

This demonstration program, written for training purposed by Graham Ellis of Well House Consultants, allows a standard query to be embedded into a configuration file that can't be altered by the user, and then allows the user to select which rows and columns are required on the report. The configuration file also allows the specification of what inputs the user has in making selections - columns can be turned on or off by default, and the criteria for row selection can also be limited.

If you want to try out this demo, you may do so; we've loaded it onto this website at /adhoc/adhoc_sql_query_engine.php and it's got a login name of "demo" with a password of "xxx111".. If you would like to see the source code of all the files, there are links to them from the module description
System Overview This demonstration is based on a 4 layer model, keeping the main application, web helper routines, business logic and HTML templates each in its own file. The top layer is the file adhoc_sql_query_engine.php and you'll find that it

1. Establishes / picks up the current session
2. Checks inputs made on the form just enters
3. Decides which is the next form
4. Prepares for the next form
5. Completes and sends out the next template

All pages in the system are numbered - and that number is tracked in the variable $current. A simple run of the system would be:
Page 1 - Login
Page 10 - Select a default or tuned query
Page 11 - Select the ROWS you want
Page 12 - Select the COLUMNS you want
Page 20 - Display results
Page 199 - Logout

You'll go straight from page 10 to page 20 if you select the default query, and you're able to jump back from page 20 to page 10 if you want to run a series of queries without logging in many times over.
Extensions
If this demonstration was extended, logins would be validated against a database (and not just hard coded) and perhaps there would be a "lost password" system too.
Help page 1 - Ad Hoc query system
Well House Consultants, 2024