一个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 gd2 上传图片/文字水印/图片水印/等比例缩略图/实现代码
May 15 PHP
php array的学习笔记
May 10 PHP
PHP写的求多项式导数的函数代码
Jul 04 PHP
CodeIgniter框架中_remap()使用方法2例
Mar 10 PHP
php实现微信公众平台账号自定义菜单类
Oct 11 PHP
解析WordPress中控制用户登陆和判断用户登陆的PHP函数
Mar 01 PHP
在Yii2中使用Pjax导致Yii2内联脚本载入失败的原因分析
Mar 06 PHP
PHP使用stream_context_create()模拟POST/GET请求的方法
Apr 02 PHP
session 加入redis的实现代码
Jul 15 PHP
PHP实现微信JS-SDK接口选择相册及拍照并上传的方法
Dec 05 PHP
PHP中CheckBox多选框上传失败的代码写法
Feb 13 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
Joomla调用系统自带编辑器的实现方法
2016/05/05 PHP
jQuery Jcrop插件实现图片选取功能
2011/11/23 Javascript
animate动画示例(泪奔的小孩)及stop和delay的使用
2013/05/06 Javascript
Jquery显示、隐藏元素以及添加删除样式
2013/08/09 Javascript
Extjs表单常见验证小结
2014/03/07 Javascript
实现placeholder效果的方案汇总
2015/06/11 Javascript
JavaScript的ExtJS框架中表格的编写教程
2016/05/21 Javascript
原生JS实现几个常用DOM操作API实例
2017/01/19 Javascript
node操作mysql数据库实例详解
2017/03/17 Javascript
jQuery中map函数的两种方式
2017/04/07 jQuery
ES6新特性八:async函数用法实例详解
2017/04/21 Javascript
JS实现无缝循环marquee滚动效果
2017/05/22 Javascript
微信小程序 es6-promise.js封装请求与处理异步进程
2017/06/12 Javascript
JavaScript判断浏览器和hack滚动条的写法
2017/07/23 Javascript
javascript 构建模块化开发过程解析
2019/09/11 Javascript
vue+导航锚点联动-滚动监听和点击平滑滚动跳转实例
2019/11/13 Javascript
es6 for循环中let和var区别详解
2020/01/12 Javascript
Python开发实例分享bt种子爬虫程序和种子解析
2014/05/21 Python
python中遍历文件的3个方法
2014/09/02 Python
python读取word文档的方法
2015/05/09 Python
python获取array中指定元素的示例
2019/11/26 Python
Python 中的pygame安装与配置教程详解
2020/02/10 Python
python实现不同数据库间数据同步功能
2021/02/25 Python
浅谈Python xlwings 读取Excel文件的正确姿势
2021/02/26 Python
伊芙丽官方旗舰店:中国淑女一线品牌
2017/12/01 全球购物
外企求职信范文分享
2013/12/31 职场文书
医药销售求职信范文
2014/02/01 职场文书
数学与统计学院学生个人职业生涯规划书
2014/02/10 职场文书
行政管理毕业生自荐信
2014/02/24 职场文书
生日寄语大全
2014/04/08 职场文书
日语专业求职信
2014/07/04 职场文书
新闻发布会活动策划方案
2014/09/15 职场文书
男生贾里读书笔记
2015/06/30 职场文书
大学生干部培训心得体会
2016/01/06 职场文书
拒绝盗图!教你怎么用python给图片加水印
2021/06/04 Python
如何通过cmd 连接阿里云服务器
2022/04/18 Servers