一个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递归调用的小技巧讲解
Feb 19 PHP
php实现的通用图片处理类
Mar 24 PHP
PHP遍历数组的方法汇总
Apr 30 PHP
PHP SPL标准库之接口(Interface)详解
May 11 PHP
php中 $$str 中 &quot;$$&quot; 的详解
Jul 06 PHP
php有效防止同一用户多次登录
Nov 19 PHP
PHP可变函数学习小结
Nov 29 PHP
Zend Framework教程之Zend_Helpers动作助手ViewRenderer用法详解
Jul 20 PHP
PHP中模糊查询并关联三个select框
Jun 19 PHP
PHP实现的最大正向匹配算法示例
Dec 19 PHP
laravel框架中表单请求类型和CSRF防护实例分析
Nov 23 PHP
Thinkphp 框架基础之入口文件功能、定义与用法分析
Apr 27 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
建立动态的WML站点(二)
2006/10/09 PHP
根据中文裁减字符串函数的php代码
2013/12/03 PHP
浅析iis7.5安装配置php环境
2015/05/10 PHP
CodeIgniter配置之routes.php用法实例分析
2016/01/19 PHP
php结合md5的加密解密算法实例
2016/09/30 PHP
laravel orm 关联条件查询代码
2019/10/21 PHP
静态的动态续篇之来点XML
2006/12/23 Javascript
JS 获取浏览器和屏幕宽高等信息的实现思路及代码
2013/07/31 Javascript
JavaScript—window对象使用示例
2013/12/09 Javascript
在JavaScript中判断整型的N种方法示例介绍
2014/06/18 Javascript
js获取元素外链样式的方法
2015/01/27 Javascript
jQuery热气球动画半透明背景的后台登录界面代码分享
2015/08/28 Javascript
BootStrap中Tab页签切换实例代码
2016/05/30 Javascript
AngularJS报错$apply already in progress的解决方法分析
2017/01/30 Javascript
vue.js树形组件之删除双击增加分支实例代码
2017/02/28 Javascript
Node.js之网络通讯模块实现浅析
2017/04/01 Javascript
Nautil 中使用双向数据绑定的实现
2019/10/02 Javascript
Python调用C/C++动态链接库的方法详解
2014/07/22 Python
python构建深度神经网络(DNN)
2018/03/10 Python
使用python编写udp协议的ping程序方法
2018/04/22 Python
Python3中_(下划线)和__(双下划线)的用途和区别
2019/04/26 Python
Python中函数参数匹配模型详解
2019/06/09 Python
Python爬虫 批量爬取下载抖音视频代码实例
2019/08/16 Python
Django 创建后台,配置sqlite3教程
2019/11/18 Python
tensorflow 分类损失函数使用小记
2020/02/18 Python
Python之多进程与多线程的使用
2021/02/23 Python
详解HTML5中ol标签的用法
2015/09/08 HTML / CSS
澳大利亚人信任的清洁平台,您的私人管家:Jarvis
2020/12/25 全球购物
大学生优秀的自我评价分享
2013/10/22 职场文书
超市营业员岗位职责
2013/12/20 职场文书
党员对照检查剖析材料
2014/10/13 职场文书
2014年个人教学工作总结
2014/12/09 职场文书
党员个人总结范文
2015/02/14 职场文书
php微信小程序解包过程实例详解
2021/03/31 PHP
如何在CSS中绘制曲线图形及展示动画
2021/05/24 HTML / CSS
SQL Server 中的事务介绍
2022/05/20 SQL Server