喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 帮助

RSS 订阅当前论坛  

喜悦证交所已经关闭

上一主题 下一主题
     
标题: [代码] 一个mysql类  
 
04007147
新手上路
Rank: 1


UID 90668
精华 0
积分 13
帖子 12
金钱 13 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2007-4-3
状态 离线
[广告]: q m
一个mysql类

超越PHP上看到的一个MYSQL类,觉得不错

<?php
class MYSQL
{
    var $no_rows=0, $row=array();
    var $no_fields=0, $field=array();

    //#-----------------------------------------------------------------
    //#---- FUNCTION :: MYSQL($p_host, $p_user, $p_passwd, $p_db="mysql")
    //#---- DESCRIPTION  ::
    //#----      Initialize class with information to access server
    //#----      No connection will be made at this point.
    //#---- INPUT ::
    //#----      p_host      : server hostname|IP address
    //#----      p_user      : user name to log into server
    //#----      p_passwd    : passwd for the user
    //#----      p_db        : database to be used
    //#---- OUTPUT ::
    //#----      none
    //#-----------------------------------------------------------------
    function MYSQL($p_host, $p_user, $p_passwd, $p_db="mysql")
    {
        $this->sql_host = $p_host;
        $this->sql_user= $p_user;
        $this->sql_passwd = $p_passwd;
        $this->sql_db = $p_db;
    } // end MYSQL

    //#-----------------------------------------------------------------
    //#---- FUNCTION :: RunDB($statement, $exp_result = "")
    //#---- DESCRIPTION ::
    //#----      Execute a MySQL statement in a non-persistant mode
    //#---- INPUT    ::
    //#----      p_statement : statement to be executed
    //#----      exp_result  : is result expected?
    //#----           value 1 (default): result stored in row array
    //#----           value 0: result not stored in row array
    //#---- OUTPUT   ::
    //#----      return "OK"        : succesful
    //#----      return err_message from mysql_connect
    //#----      exp_result==1: additional result stored into array row
    //#----          no_row contains no. of record retrieved
    //#----                row[recno][ "field" ] contains value of recno record
    //#----          field["fieldname"] contains the field list
    //#-----------------------------------------------------------------
    function RunDB($p_statement, $exp_result = 1)
    {
        //--- 连接到数据库
        $link=mysql_connect($this->sql_host, $this->sql_user, $this->sql_passwd);
        if (!$link)
            return sprintf("error connecting to host %s, by user %s",
                           $this->sql_host, $this->sql_user) ;
        //--- 选择数据库
        if (!mysql_select_db($this->sql_db, $link))
        {   $err_msg=sprintf("Error in selecting %s database",
                     $this->sql_db);
            $err_msg .= sprintf("error:%d %s", mysql_errno($link),
                     mysql_error($link));
            return $err_msg;
          }
        //--- 执行查询
        if (!($this->result=mysql_query($p_statement, $link)))
        {   $err_msg=sprintf("Error in selecting %s database\n",
                     $this->sqldb);
            $err_msg .= sprintf("\terror:%d\t\nerror message %s",
                        mysql_errno($link), mysql_error($link));
            return $err_msg;
        }
        //--- Organize the result
                if (!preg_match("/^\s*(delete|insert|replace|update)/i", $p_statement))
                {if ($exp_result == 1)
        {   $this->no_rows = mysql_num_rows($this->result);
            $this->GroupResult();
        }}
        //--- SUCCESS RETURN
        return OK;
    } // end function RunDB


    //#-----------------------------------------------------------------
    //#---- FUNCTION :: GroupResult( )
    //#---- DESCRIPTION ::
    //#----      To group the raw result retrieved in an associative array
    //#----      A query has to be made using RunDB prior to this execution
    //#----      The handle is storedin $result
    //#---- INPUT    :: None
    //#---- OUTPUT   :
    //#----      return none
    //#----      additional result stored into array
    //#----          no_row, row[recno]["field"] = value
    //#----          no_field, field["fieldname"]
    //#-----------------------------------------------------------------
    function GroupResult()
    {
        //--- Get RESULT
        $is_header = FALSE;
        for ( $recno = 0; $recno < $this->no_rows; $recno ++ )
        {   $row = mysql_fetch_object($this->result);
            //--- Get Field List
            if ( ! $is_header )
            {   $no_fields = 0;
                $t_row = $row;
                while ( $item = each($t_row) )
                {   $this->field[$no_fields] = $item["key"];
                    $no_fields ++;
                }
                $this->no_fields = $no_fields;
                $is_header = TRUE;
            }
            //---- GET DATA
            while ( $item = each($row))
                $this->row[$recno][$item["key"]] = $item["value"];
        }
        //--- END CONNECTION
        mysql_free_result($this->result);
    } // GroupResult

    //#-----------------------------------------------------------------
    //#---- FUNCTION :: ShowHTML($p_table="", $p_header = "", $p_cell = "")
    //#---- DESCRIPTION ::
    //#----      To return the result in HTML Table format
    //#---- INPUT    ::
    //#----      p_table    : HTML <Table> format
    //#----      p_header   : First row format
    //#----      p_cell     : Individual cell format
    //#---- OUTPUT   ::
    //#----      "OK"        : succesful
    //#----      err_message from mysql_connect
    //#-----------------------------------------------------------------
   
        function ShowHTML($p_table="", $p_header="", $p_cell="" )
    {
        //--- DEFAULT OPTION
        $p_table=($p_table=="")?"BGCOLOR=#BB9999 BORDER=1": $p_table;
        $p_header=($p_header=="")? "BGCOLOR=#9999BB" : $p_header;
        $p_cell=($p_cell=="")?"BGCOLOR=#99BB99":$p_cell;
        //--- DISPLAY TABLE
        echo "<TABLE ".$p_table.">";
        //--- DISPLAY HEADER LINE
        echo "<TR ".$p_header.">";
        echo "<TD>recno";
        for ($i = 0; $i < $this->no_fields; $i ++)
            printf("<TD>%s", $this->field[$i]);
        //--- DISPLAY DATA
        for ( $i = 0; $i < $this->no_rows; $i ++)
        {   echo "<TR $p_cell>";
            printf("<TD>%-3s", $i);
            for ($f = 0; $f < $this->no_fields; $f ++)
            {   $f_name = $this->field[$f];
                $f_value = $this->row[$i][$f_name];
                if ( $f_value=="" )
                    $f_value="&nbsp;";
                printf("<TD>%s", $f_value);
            }
        }
        //--- THE END
        echo "</TABLE>";
    } // ShowHTML
        function ShowXml($table,$no)
        {
        $sql="slect * from `$table` LIMIT 0 , $no";
        RunDb($sql);
           for ($i = 0; $i < $mysql->no_rows; $i ++)
    {
        echo "Record No: " . ($i + 1) ."<HR>";
        for ($j = 0; $j < $mysql->no_fields; $j ++)
        {
            $field_name = $mysql->field[$j];
            echo "Field: ".$field_name."  ----- Value: ".
                $mysql->row[$i][$field_name]."<BR>";
        }
    }
        }

} // end class MYSQL
?>
这是一个小应用,
用来把数据库的数据生成xml
mysqlxml1.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="mysqlxml.php">
  <label>请选择生成XML的表
  <select name="table" id="table">
    <option value="教师">教师</option>
    <option value="学生">学生</option>
    <option value="助教">助教</option>
    <option value="教师授课">教师授课</option>
    <option value="课程">课程</option>
    <option value="成绩">成绩</option>
    <option value="选课">选课</option>
  </select>
  </label>
  <label>
  <input type="submit" name="Submit" value="提交" />
  </label>
</form>
</body>
</html>
mysqlxml.php

<?php
$table=$_POST['table'];
include("class_mysql.php");
$mysql=new MYSQL("localhost", "pjq", "pjq", "sys");
$status = $mysql->RunDb("select * from $table");
        if ($status != "OK")
    {   echo "<HR>DB Error: $status.<HR>";
        die;
  }
header("Content-Type: text/xml");  
echo"<?xml version=\"1.0\" encoding=\"GB2312\"?>\n";
        echo"<".$table.">\n";
        for($i = 0; $i < $mysql->no_rows; $i ++)
        {
        echo"<item>\n";
        for ($j = 0; $j < $mysql->no_fields; $j ++)
        {
            $field_name = $mysql->field[$j];
            echo "<".$field_name.">".$mysql->row[$i][$field_name]."</".$field_name.">\n";
               
        }
        echo"</item>\n";       
                }
       
    echo"</".$table.">\n";

?>
2007-4-17 05:05 PM#1
查看资料  访问主页  Blog  发短消息  QQ  顶部
 
chinahtml
金牌会员
Rank: 6Rank: 6


UID 69683
精华 0
积分 1012
帖子 1019
金钱 1012 喜悦币
威望 0
人脉 0
阅读权限 70
注册 2006-3-16
状态 离线
[推荐阅读] 谁有PHP的排列组合代码
还可以,不过好像不支持Mysql5



-地振山冈,一派溪山千古秀
-门朝大海,山合河水万年流
2007-4-17 10:07 PM#2
查看资料  发短消息  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.028326 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP