PHP Classes
elePHPant
Icontem

File: test.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Vadim Sachenko  >  MM::CMySQL  >  test.php  >  Download  
File: test.php
Role: Example script
Content type: text/plain
Description: Test script
Class: MM::CMySQL
Execute parameterized MySQL database queries
Author: By
Last change:
Date: 9 years ago
Size: 2,437 bytes
 

Contents

Class file image Download
<?php
// ----------------------------------------------------------------------------
//
// Script for testing the following php-modules: mmlocal.php, mmdb.php,
// mmlog.php, mmmysql.php.
//
// This file is available under The MIT License
// http://www.sachenko.com
// Copyright (c) 2005 Vadim Sachenko
//
// ----------------------------------------------------------------------------

include ("mm/mmmysql.php");

// define username ($sMysqlUser) and password ($sMysqlPass)
// for mysql connection in this file
include ("passwd.php");

$log = new CLog;

$sName = $_POST["name"];
$sParams = $_POST["params"];
$sSQL = $_POST["sql"];

if(
$sSQL)
{
   
$sSQL = ltrim(stripslashes($sSQL), "\r\n");
    if(
substr($sSQL, 0, 2) != "--")
    {
       
$sName = "default";
       
$sSQL = "--default\r\n" . $sSQL;
    }
   
   
$sFile = tempnam("/tmp", "sqlfile");
   
   
$fp = fopen($sFile, "w");
   
fputs($fp, $sSQL);
   
fclose($fp);
   
   
$query = new CMySQL("mmtest@localhost", $sMysqlUser, $sMysqlPass);
   
    if(
$log->IsEmpty())
    {
   
       
$query->SetFile($sFile);
   
       
$aParams = array();
   
       
$asParams = explode(";", $sParams);
        for(
$I = 0; $I < count($asParams); $I++)
        {
           
$asParam = explode("=", $asParams[$I], 2);
           
$aParams[$asParam[0]] = $asParam[1];
        }
   
       
$query->FileQuery($sName, $aParams);
   
       
$sResult = "";
   
       
$asRecord = $query->Fetch();
        if(
$asRecord)
        {
           
$sResult .= "<tr>";
            foreach(
$asRecord as $sKey => $sValue)
            {
               
$sResult .= "<th>$sKey";
            }
           
$sResult .= "</tr>\r\n";
        }
        while(
$asRecord)
        {
           
$sResult .= "<tr>";
            foreach(
$asRecord as $sValue)
            {
               
$sResult .= "<td>$sValue";
            }
           
$sResult .= "</tr>\r\n";
       
           
$asRecord = $query->Fetch();
        }
   
       
$query->Close();
       
unlink($sFile);
   
        if(
$sResult)
        {
           
$sResult = "<TABLE border=1><caption>Result</caption>$sResult</TABLE>";
        }
    }
}

?>
<HTML>
<HEAD>
  <TITLE>Test MM::PHP::Lib::MySQL</TITLE>
</HEAD>
<BODY>
<?php print $sResult; ?>
<FORM method=post>
    Query name<br>
    <input name=name value='<?php print $sName; ?>'></input><br>
    Parameters. Format: name=value;...<br>
    <input name=params value='<?php print $sParams; ?>'></input><br>
    sql-file content<br>
    <TEXTAREA rows=10 cols=80 name=sql><?php print $sSQL; ?></TEXTAREA><br><br>
    <input type=submit value="Execute SQL query"></input>
  </FORM>
<?php $log->Display(); ?>
</BODY>
</HTML>