一个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 相关文章推荐
支持oicq头像的留言簿(二)
Oct 09 PHP
基于mysql的论坛(7)
Oct 09 PHP
dedecms 制作模板中使用的全局标记图文教程
Mar 11 PHP
php ss7.5的数据调用 (笔记)
Mar 08 PHP
php中使用接口实现工厂设计模式的代码
Jun 17 PHP
ThinkPHP使用UTFWry地址库进行IP定位实例
Apr 01 PHP
php防止恶意刷新与刷票的方法
Nov 21 PHP
php中随机函数mt_rand()与rand()性能对比分析
Dec 01 PHP
php格式化电话号码的方法
Apr 24 PHP
PHP基于DOM创建xml文档的方法示例
Feb 08 PHP
PHP调用Mailgun发送邮件的方法
May 04 PHP
strpos() 函数判断字符串中是否包含某字符串的方法
Jan 16 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
使用PHP数组实现无限分类,不使用数据库,不使用递归.
2006/12/09 PHP
在PHP中养成7个面向对象的好习惯
2010/01/28 PHP
php中日期加减法运算实现代码
2011/12/08 PHP
PHP代码优化的53个细节
2014/03/03 PHP
PHP实现的简单三角形、矩形周长面积计算器分享
2014/11/18 PHP
PHP处理postfix邮件内容的方法
2015/06/16 PHP
PHP 记录访客的浏览信息方法
2018/01/29 PHP
phpstudy2020搭建站点的实现示例
2020/10/30 PHP
JavaScript学习笔记(二) js对象
2011/10/25 Javascript
JS数组的遍历方式for循环与for...in
2014/07/31 Javascript
alert出数组中的随即值代码
2014/09/25 Javascript
node.js中的fs.mkdir方法使用说明
2014/12/17 Javascript
JavaScript获取文本框内选中文本的方法
2015/02/20 Javascript
jQuery+Ajax实现无刷新操作
2016/01/04 Javascript
js实现上传图片及时预览
2016/05/07 Javascript
AngularJS基础 ng-value 指令简单示例
2016/08/03 Javascript
JS实现的表头列头固定页面功能示例
2017/01/10 Javascript
js canvas实现擦除效果示例代码
2017/04/26 Javascript
使用JavaScript实现点击循环切换图片效果
2017/09/03 Javascript
基于Vue实现图片在指定区域内移动的思路详解
2018/11/11 Javascript
JavaScript设计模式之观察者模式实例详解
2019/01/16 Javascript
vue实现短信验证码登录功能(流程详解)
2019/12/10 Javascript
JavaScript switch语句使用方法简介
2019/12/30 Javascript
vue2路由基本用法实例分析
2020/03/06 Javascript
javascript设计模式 ? 观察者模式原理与用法实例分析
2020/04/22 Javascript
使用vue3重构拼图游戏的实现示例
2021/01/25 Vue.js
Python3生成手写体数字方法
2018/01/30 Python
基于DataFrame改变列类型的方法
2018/07/25 Python
Python学习笔记之字符串和字符串方法实例详解
2019/08/22 Python
使用IPython或Spyder将省略号表示的内容完整输出
2020/04/20 Python
使用CSS3在触屏上为按钮实现激活效果
2013/09/27 HTML / CSS
美国家具网站:Cymax
2016/09/17 全球购物
.net工程师笔试题
2012/06/09 面试题
《三顾茅庐》教学反思
2014/04/10 职场文书
鼓舞士气的口号
2014/06/16 职场文书
2014年度安全工作总结
2014/12/04 职场文书