一个SQL管理员的web接口


Posted in PHP onOctober 09, 2006

<?   
    /*************************************************************************************  
     *        SQLAdmin  v2.0  -  An  SQL  Administration  User  Interface  for  the  Web *   
     *            Copyright  (C)  1997-98    Alessandro  Vernet  <avernet@scdi.org>      *   
     *************************************************************************************   
     *      This  library  is  free  software;  you  can  redistribute  it  and/or       *   
     *      modify  it  under  the  terms  of  the  GNU  Library  General  Public        *   
     *      License  as  published  by  the  Free  Software  Foundation;  either         *   
     *      version  2  of  the  License,  or  (at  your  option)  any  later  version.  *   
     *                                                                                   *   
     *      This  library  is  distributed  in  the  hope  that  it  will  be  useful,   *   
     *      but  WITHOUT  ANY  WARRANTY;  without  even  the  implied  warranty  of      *   
     *      MERCHANTABILITY  or  FITNESS  FOR  A  PARTICULAR  PURPOSE.    See  the  GNU  *   
     *      Library  General  Public  License  for  more  details.                       *   
     *                                                                                   *   
     *      You  should  have  received  a  copy  of  the  GNU  Library  General  Public *   
     *      License  along  with  this  library;  if  not,  write  to  the               *   
     *      Free  Software  Foundation,  Inc.,  59  Temple  Place  -  Suite  330,        *   
     *      Boston,  MA    02111-1307,  USA.                                             *   
     *************************************************************************************/   

    /*  TODO:   
      *  -  Add  sort  order.   
      *  -  Add  simple  view.   
      *  -  Add  some  documentation.   
      */   

    /*  LIMITATIONS:   
      *  -  Works  only  with  mSQL.   
      */   

    /*  HISTORY:   
      *  -  97-11-05  (avernet)  Corrected  a  bug  with  quote.   
      *  -  98-01-01  (avernet)  Added  a  sortColumn  parameter  to   
      *      administrationTable  function.   
      *  -  98-03-14  (avernet)  Added  function  addTable  to  enable  users  to   
      *      add  (but  not  modify)  en  entry  to  the  database.   
      *  -  98-05-19  (avernet)  Submitted  to  PX.   
      *  -  98-10-11  (avernet)  Now  SQLAdmin  works  with  PHP3.  The  PHP2  version   
      *      will  not  be  mainteained  anymore.   
      *  -  98-10-11  (avernet)  SQLAdmin  is  now  distributed  under  the  LGPL   
      *      instead  of  MPL.   
      */   

    function  escapeforhtml  ($string)   
    {   
        $result  =  $string;   
        //$result  =  ereg_replace  ("\"",  """,  $result);   
        $result  =  ereg_replace  ("<",  "<",  $result);   
        $result  =  ereg_replace  (">",  ">",  $result);   
        return  $result;   
    }   

    function  displayTuple  ($fieldsNumber,  $fieldNames,   
                                                  $fieldLengths,  $values,  $mode)   
    {   
        $result  =  "";   
        $result  .=  "<FORM  METHOD=\"post\"><TABLE  BORDER><TR>"  .   
            "<TD  BGCOLOR=\"#CCCCFF\">";   
        $result  .=  "<TABLE  CELLSPACING=\"0\"  CELLPADDING=\"0\">";   
        $fieldIndex  =  0;   
        while  ($fieldIndex  <  $fieldsNumber)   
        {   
            $result  .=  "<TR><TD>"  .  $fieldNames  [$fieldIndex]  .  "</TD><TD>";   
            if  ($fieldLengths  [$fieldIndex]  <=  128)   
            {   
                $result  .=  "<INPUT  TYPE=\"text\"  NAME=\""  .   
                    $fieldNames  [$fieldIndex]  .  "\"  VALUE=\""  .   
                    $values  [$fieldIndex]  .  "\"  SIZE=\"64\">";   
            }   
            else   
            {   
                $result  .=  "<TEXTAREA  NAME=\""  .   
                    $fieldNames  [$fieldIndex]  .  "\""  .   
                    "  COLS=\"64\"  ROWS=\"10\"  WRAP=\"virtual\">"  .   
                    escapeforhtml  ($values  [$fieldIndex])  .  "</TEXTAREA>";   
            }   
            $result  .=    "<INPUT  TYPE=\"hidden\"  NAME=\"old-"  .   
                $fieldNames  [$fieldIndex]  .   
                "\"  VALUE=\""  .  escapeforhtml  ($values  [$fieldIndex])  .  "\">"  .   
                "</TD></TR>";   
            $fieldIndex++;   
        }   
        $result  .=  "<TR><TD  ALIGN=\"center\"  COLSPAN=\"2\">";   
        if  ($mode  ==  "modify")   
        {   
            $result  .=  "<INPUT  TYPE=\"submit\"  NAME=\"remove\"  VALUE=\"Remove\">";   
            $result  .=  "<INPUT  TYPE=\"submit\"  NAME=\"update\"  VALUE=\"Update\">";   
        }   
        else   
            {  $result  .=  "<INPUT  TYPE=\"submit\"  NAME=\"add\"  VALUE=\"Add\">";  }   
        $result  .=  "</TABLE></TD></TR></TABLE></FORM>";   
        return  $result;   
    }   

    function  fieldFromType  ($text,  $type)   
    {   
        if  ($type  ==  "int"  ||  $type  ==  "uint"  ||  $type  == "real")   
            {  $result  =  $text;  }   
        else   
            {  $result  =  "'"  .  AddSlashes  ($text)  .  "'";  }   
        return  $result;   
    }   

    function  executeMsql  ($database,  $command)   
    {   
        /*echo  "<TT>"  .  $command  .  "</TT><HR>";*/   
        msql  ($database,  $command);   
    }   

    function  handleRemove  ($database,  $table,  $fieldsNumber,   
                                                  $fieldNames,  $fieldLengths,  $fieldTypes)   
    {   
        global  $remove;   
        if  ($remove  !=  "")   
        {   
            $command  =  "DELETE  FROM  "  .  $table  .  "  WHERE  ";   
            $fieldIndex  =  0;   
            while  ($fieldIndex  <  $fieldsNumber)   
            {   
                $fieldName  =  "old-"  .  $fieldNames  [$fieldIndex];   
                global  $$fieldName;   
                $command  .=  $fieldNames  [$fieldIndex]  .  "="  .   
                    fieldFromType  ($$fieldName,  $fieldTypes  [$fieldIndex]);   
                if  ($fieldIndex  !=  $fieldsNumber  -  1)   
                    {  $command  .=  "  AND  ";  }   
                $fieldIndex++;   
            }                  
            executeMsql  ($database,  $command);   
        }   
    }   

    function  handleUpdate  ($database,  $table,  $fieldsNumber,   
                                                  $fieldNames,  $fieldLengths,  $fieldTypes)   
    {   
        global  $update;   
        if  ($update  !=  "")   
        {   
            $command  =  "UPDATE  "  .  $table  .  "  SET  ";   
            $fieldIndex  =  0;   
            while  ($fieldIndex  <  $fieldsNumber)   
            {   
                $fieldName  =  $fieldNames  [$fieldIndex];   
                global  $$fieldName;   
                $command  .=  $fieldName  .  "="  .   
                    fieldFromType  ($$fieldName,  $fieldTypes  [$fieldIndex]);   
                if  ($fieldIndex  !=  $fieldsNumber  -  1)   
                    {  $command  .=  ",  ";  }   
                $fieldIndex++;   
            }   
            $command  .=  "  WHERE  ";   
            $fieldIndex  =  0;   
            while  ($fieldIndex  <  $fieldsNumber)   
            {   
                $fieldName  =  "old-"  .  $fieldNames  [$fieldIndex];   
                global  $$fieldName;   
                $command  .=  $fieldNames  [$fieldIndex]  .  "="  .   
                    fieldFromType  ($$fieldName,  $fieldTypes  [$fieldIndex]);   
                if  ($fieldIndex  !=  $fieldsNumber  -  1)   
                    {  $command  .=  "  AND  ";  }   
                $fieldIndex++;   
            }   
            executeMsql  ($database,  $command);   
        }   
    }   

    function  handleAdd  ($database,  $table,  $fieldsNumber,   
                                            $fieldNames,  $fieldLengths,  $fieldTypes)   
    {   
        global  $add;   
        if  ($add  !=  "")   
        {   
            $command  =  "INSERT  INTO  "  .  $table  .  "  (";   
            $fieldIndex  =  0;   
            while  ($fieldIndex  <  $fieldsNumber)   
            {   
                $command  .=  $fieldNames  [$fieldIndex];   
                if  ($fieldIndex  !=  $fieldsNumber  -  1)   
                    {  $command  .=  ",  ";  }   
                $fieldIndex++;   
            }   
            $command  .=  ")  VALUES  (";   
            $fieldIndex  =  0;   
            while  ($fieldIndex  <  $fieldsNumber)   
            {   
                $fieldName  =  $fieldNames  [$fieldIndex];   
                global  $$fieldName;   
                $command  .=  fieldFromType  ($$fieldName,  $fieldTypes  [$fieldIndex]);   
                if  ($fieldIndex  !=  $fieldsNumber  -  1)   
                    {  $command  .=  ",  ";  }   
                $fieldIndex++;   
            }   
            $command  .=  ")";   
            executeMsql  ($database,  $command);   
        }   
    }   

    function  displayRemoveUpdate  ($database,  $table,  $sortColumn,   
                                                                $fieldsNumber,  $fieldNames,  $fieldLengths)   
    {   
        $result  =  "";   
        if  ($sortColumn  !=  "")   
            {  $sortColumn  =  "  ORDER  BY  "  .  $sortColumn;  }   
        $msqlresult  =  msql  ($database,  "SELECT  *  FROM  "  .  $table  .  $sortColumn);   
        $tuplesNumber  =  msql_numrows  ($msqlresult);   
        $tupleIndex  =  0;   
        while  ($tupleIndex  <  $tuplesNumber)   
        {   
            $fieldIndex  =  0;   
            while  ($fieldIndex  <  $fieldsNumber)   
            {   
                $values  [$fieldIndex]  =  msql_result  ($msqlresult,  $tupleIndex,   
                    $fieldNames  [$fieldIndex]);   
                $fieldIndex++;   
            }   
            $result  .=  displayTuple  ($fieldsNumber,  $fieldNames,   
                $fieldLengths,  $values,  "modify");   
            $tupleIndex++;   
        }   
        return  $result;   
    }   

    function  displayAdd  ($fieldsNumber,  $fieldNames,  $fieldLengths)   
    {   
        $result  =  "";   
        $fieldIndex  =  0;   
        while  ($fieldIndex  <  $fieldsNumber)   
        {   
            $values  [$fieldIndex]  =  "";   
            $fieldIndex++;   
        }   
        $result  .=  displayTuple  ($fieldsNumber,  $fieldNames,   
            $fieldLengths,  $values,  "add");   
        msql_close  ();   
        return  $result;   
    }   

    function  administrationTable  ($database,  $table,  $sortColumn)   
    {   
        $result  =  "";   
        msql_connect  ( "localhost");   
        $msqlresult  =  msql  ($database,  "SELECT  *  FROM  "  .  $table);   
        $fieldsNumber  =  msql_numfields  ($msqlresult);    
        $msqlresult  =  msql_listfields  ($database,  $table);   
        $fieldIndex  =  0;   
        while  ($fieldIndex  <  $fieldsNumber)   
        {   
            $fieldNames  [$fieldIndex]  =  msql_fieldname  ($msqlresult,  $fieldIndex);   
            $fieldLengths  [$fieldIndex]  =  msql_fieldlen  ($msqlresult,  $fieldIndex);   
            $fieldTypes  [$fieldIndex]  =  msql_fieldtype  ($msqlresult,  $fieldIndex);   
            $fieldIndex++;   
        }   
        handleRemove  ($database,  $table,  $fieldsNumber,  $fieldNames,  $fieldLengths,  $fieldTypes);   
        handleUpdate  ($database,  $table,  $fieldsNumber,  $fieldNames,  $fieldLengths,  $fieldTypes);   
        handleAdd  ($database,  $table,  $fieldsNumber,  $fieldNames,  $fieldLengths,  $fieldTypes);   
        $result  .=  displayRemoveUpdate  ($database,  $table,  $sortColumn,  $fieldsNumber,  $fieldNames,    
$fieldLengths);   
        $result  .=  displayAdd  ($fieldsNumber,  $fieldNames,  $fieldLengths);   
        return  $result;   
    }   

    function  addTable  ($database,  $table)   
    {   
        $result  =  "";   
        msql_connect  ( "localhost");   
        $msqlresult  =  msql  ($database,  "SELECT  *  FROM  "  .  $table);   
        $fieldsNumber  =  msql_numfields  ($msqlresult);    
        $msqlresult  =  msql_listfields  ($database,  $table);   
        $fieldIndex  =  0;   
        while  ($fieldIndex  <  $fieldsNumber)   
        {   
            $fieldNames  [$fieldIndex]  =  msql_fieldname  ($msqlresult,  $fieldIndex);   
            $fieldLengths  [$fieldIndex]  =  msql_fieldlen  ($msqlresult,  $fieldIndex);   
            $fieldTypes  [$fieldIndex]  =  msql_fieldtype  ($msqlresult,  $fieldIndex);   
            $fieldIndex++;   
        }   
        handleAdd  ($database,  $table,  $fieldsNumber,  $fieldNames,  $fieldLengths,  $fieldTypes);   
        $result  .=  displayAdd  ($fieldsNumber,  $fieldNames,  $fieldLengths);   
        return  $result;   
    }   
?>   

PHP 相关文章推荐
PHP 中执行系统外部命令
Oct 09 PHP
php切割页面div内容的实现代码分享
Jul 31 PHP
PHP5.3与5.5废弃与过期函数整理汇总
Jul 10 PHP
PHP之autoload运行机制实例分析
Aug 28 PHP
ThinkPHP实现静态缓存和动态缓存示例代码
May 02 PHP
Laravel使用支付宝进行支付的示例代码
Aug 16 PHP
Thinkphp5行为使用方法汇总
Dec 21 PHP
PHP设计模式之装饰器模式实例详解
Feb 07 PHP
PHP输出Excel PHPExcel的方法
Jul 26 PHP
thinkphp集成前端脚手架Vue-cli的教程图解
Aug 30 PHP
Laravel框架基于中间件实现禁止未登录用户访问页面功能示例
Jan 17 PHP
laravel框架中控制器的创建和使用方法分析
Nov 23 PHP
如何实现给定日期的若干天以后的日期
Oct 09 #PHP
模拟SQLSERVER的两个函数:dateadd(),datediff()
Oct 09 #PHP
实时抓取YAHOO股票报价的代码
Oct 09 #PHP
php访问查询mysql数据的三种方法
Oct 09 #PHP
杏林同学录(一)
Oct 09 #PHP
杏林同学录(二)
Oct 09 #PHP
基于mysql的bbs设计(五)
Oct 09 #PHP
You might like
Linux下ZendOptimizer的安装与配置方法
2007/04/12 PHP
深入PHP curl参数的详解
2013/06/17 PHP
php动态生成缩略图并输出显示的方法
2015/04/20 PHP
php 的多进程操作实践案例分析
2020/02/28 PHP
php命令行模式代码实例详解
2021/02/26 PHP
javascript中的继承实例代码
2011/04/27 Javascript
Bootstrap树形组件jqTree的简单封装
2016/01/25 Javascript
jQuery调用Webservice传递json数组的方法
2016/08/06 Javascript
JavaScript队列、优先队列与循环队列
2016/11/14 Javascript
jQuery实现拖动剪裁图片作为头像
2016/12/28 Javascript
js实现漫天星星效果
2017/01/19 Javascript
自定义事件解决重复请求BUG的问题
2017/07/11 Javascript
ReactNative之FlatList的具体使用方法
2017/11/29 Javascript
Angular5.1新功能分享
2017/12/21 Javascript
微信小程序之裁剪图片成圆形的实现代码
2018/10/11 Javascript
JS实现移动端在线签协议功能
2019/08/22 Javascript
vue实践---根据不同环境,自动转换请求的url地址操作
2020/09/21 Javascript
解决Can't find variable: SockJS vue项目的问题
2020/09/22 Javascript
17个Python小技巧分享
2015/01/23 Python
利用python循环创建多个文件的方法
2018/10/25 Python
Python3实现爬虫爬取赶集网列表功能【基于request和BeautifulSoup模块】
2018/12/05 Python
解决Django中多条件查询的问题
2019/07/18 Python
python可视化实现KNN算法
2019/10/16 Python
wxPython实现带颜色的进度条
2019/11/19 Python
使用Python实现Wake On Lan远程开机功能
2020/01/22 Python
使用HTML5进行SVG矢量图形绘制的入门教程
2016/02/19 HTML / CSS
创建索引时需要注意的事项
2013/05/13 面试题
认识深刻的检讨书
2014/02/16 职场文书
《月球之谜》教学反思
2014/04/10 职场文书
安全标语口号
2014/06/09 职场文书
检察机关个人对照检查材料
2014/09/15 职场文书
科级干部群众路线教育实践活动个人对照检查材料
2014/09/19 职场文书
关于环保的广播稿
2015/12/17 职场文书
文艺有韵味的诗句(生命类、亲情类...)
2019/07/11 职场文书
解读Vue组件注册方式
2021/05/15 Vue.js
解决Navicat for Mysql连接报错1251的问题(连接失败)
2021/05/27 MySQL