4573l2.txt

Listing 2. mysql.php

<%//////////// mysql.php /////////////////////////////

include ("class.FastTemplate.php");
$tpl = new FastTemplate(".");

$tpl->define(array( main        => "main.tpl",
                    body        => "body.tpl",
                    table       => "table.tpl",
                    headerdata  => "headerdata.tpl",
                    row         => "row.tpl",
                    rowdata     => "rowdata.tpl",
                    submit      => "submit.tpl",
                    form2       => "form2.tpl"));

$tpl->assign(array( TITLE       => "PHP SQL Code Test Results",
                    SUBMIT_NAME => "SUBMIT",
                    SUBMIT_VAL  => "NEW QUERY",
                    FORM_ACTION => "index.php",
                    STRING1     => "RESULTS OF QUERY: ",
                    QUERY       => "$query",
                    FORM_METHOD => "POST"));

                    //Assumes MySQL.
$user="bill";       //Adjust for your
$host="localhost";  //setup in these
$password="megan";  //three lines.

mysql_connect($host,$user,$password);
mysql_select_db($database);

if(!strcmp($query, "")){   //if no query, supply a generic one
    $query = "SHOW TABLES";
    }

$query = stripSlashes($query);
$result = mysql_query($query);

// append table header cells to header row.

for ($i = 0; $i < mysql_num_fields($result); $i++) {
    $thval = mysql_field_name($result,$i);
    $tpl->assign(array(HEADERDATA => "$thval"));
    $tpl->parse(HEADER, ".headerdata");
    }

// append row cells to the row, and
// then append rows to table body.

for ($i = 0; $i < mysql_num_rows($result); $i++) {
    $row_array = mysql_fetch_row($result);
    for ($j = 0; $j < mysql_num_fields($result); $j++) {
        $trval =  $row_array[$j];
        $tpl->assign(array(ROWDATA => "$trval"));
        $tpl->parse(ROW, ".rowdata");
        }
    $tpl->parse(ROWS, ".row");
    $tpl->clear("ROW");  //<- comment this line out
    }                    //   observe the results.

$tpl->parse (TABLE, "table");
$tpl->parse (SUBMIT, "submit");

$tpl->assign(array(SUBMIT_NAME => "goodbye",
                   SUBMIT_VAL  => "EXIT PROGRAM"));

$tpl->parse (GOODBYE, "submit");
$tpl->parse (FORM2, "form2");
$tpl->parse (BODY, "body");
$tpl->parse (MAIN, "main");

$tpl->FastPrint();
%>
