Custom Query Interface - HELP!

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have been trying to build an custom report interface for a charity
manangement system; which is part of my dissertation. This interface
will allow the chairty to input a SQL query and submit the query. When
the submit button is executed, it will display the selected column names
and the query data. I am 80% finished but I have encountered a problem
though. I do not know how to display the dynamic column names?! Could
some please enlighten me as I feel like giving up! I can't find the
funtion/code I need!!!! HELP!!!!

Re: Custom Query Interface - HELP!

Quoted text here. Click to load it

May depend on what DB you are using...

I use Postgres and in the docs you will notice functions to get the
number of fields returned by a query and also functions to return the
field name for a numbered field.

Iterate over the field names once to build your table header and then
go on to output the rows/fields as needed.

Assuming the other DB APIs offer same functionality, but no guarantee.


Jerry Sievers   305 854-3001 (home)     WWW ECommerce Consultant
                305 321-1144 (mobile /

Re: Custom Query Interface - HELP!

James Perry wrote:

Quoted text here. Click to load it

This was my attempt when looking at some PEAR classes:


  * Short script for dumping stuff out of a database

require_once 'HTML/Page.php';
require_once 'HTML/QuickForm.php';
require_once 'HTML/Table.php';
require_once 'DB.php';

$p = new HTML_Page();
$f = new HTML_QuickForm('irm_dumper');

$f->addElement('header',NULL,'Submit your query');
$f->addElement('text','query_string','SQL Query (SELECT only)',
array('size' => 80));
$f->addElement('reset',NULL,'Reset query');


if ($_POST['query_string']) {

    $dsn = array('phptype' => 'mysql',
                 'hostspec' => '',
                 'username' => 'usr_name_here',
                 'password' => 'usr_passwd',
                 'database' => 'your_db_name');

    $db =& DB::connect($dsn);

    if (DB::isError($db)) { die ($db->getMessage()); }
    else {

        if (!preg_match("/^(UPDATE|INSERT)/i",$_POST['query_string'])) {

            $query = stripslashes($_POST['query_string']);

            $sth =& $db->query($query);

            if (DB::isError($sth)) {
                $p->addBodyContent("<p>ERROR<br>Invalid SQL query!</p>");
                $p->addBodyContent("<p>" . $sth->getMessage() . "</p>");
            else {
                $t_attributes = array('rules' => 'all',
                                      'frame' => 'border');

                $t = new HTML_Table($t_attributes);

                $db_header = $db->tableInfo($sth);

                $headers = array();

                foreach ($db_header as $c => $c_name) {
                    $headers = array_merge($headers,array($c_name['name']));

                $header_attr = array('bgcolor' => 'steelblue');


                $align_attr = array ('valign' => 'top');
                for ($row=0; $row < $sth->numRows(); $row++) {
                    $t->addRow($sth->fetchRow(), $align_attr);

                $light = array('bgcolor' => '#CCCCCC');
                $dark = array('bgcolor' => '#AAAAAA');
        else {
            $p->addBodyContent("<p>Sorry, only SELECT queries allowed!</p>");


Site Timeline