首页 php教程 php手册 一介简介的Mysql封装类

一介简介的Mysql封装类

Jun 21, 2016 am 09:06 AM
gt mysql nbsp this

mysql|封装

class_mysql.php

//######################################################################
//##### TITLE       :: CLASS MYSQL
//##### FILE        :: class_mysql.php
//##### PROJECT     :: WebVision
//##### RELATED DOCUMENT :: None
//##### DESCRIPTION   ::
//#####     To provide access utility for MySQL access
//#####     RunDB is used to run SQL query with the result
//#####     grouped into array.
//##### AUTHOR      :: Mark Quah
//##### REVISION  ::
//######################################################################

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)
    {
        //--- Connect to the Database
        $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) ;
        //--- Select the Database
        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;
   }
        //--- Execute the Statement
        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 ($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 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

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 "
";
        //--- DISPLAY HEADER LINE
        echo "";
        echo "";
            printf("
recno";
        for ($i = 0; $i no_fields; $i ++)
            printf("
%s", $this->field[$i]);
        //--- DISPLAY DATA
        for ( $i = 0; $i no_rows; $i ++)
        {   echo "
%-3s", $i);
            for ($f = 0; $f no_fields; $f ++)
            {   $f_name = $this->field[$f];
                $f_value = $this->row[$i][$f_name];
                if ( $f_value=="" )
                    $f_value=" ";
                printf("
%s", $f_value);
            }
        }
        //--- THE END
        echo "
";
    } // ShowHTML

} // end class MYSQL
?>

例子:

    include("class_mysql.php");

    //===== set up sql connection
    $mysql=new MYSQL("server", "mysql userid", "mysql passwd", "mysql DB");

    //==== Extract Result
    $status = $mysql->RunDB("select * from user;");
    if ($status != "OK")
    {   echo "


DB Error: $status.
";
        die;
    }
    for ($i = 0; $i no_rows; $i ++)
    {
        echo "Record No: " . ($i + 1) ."
";
        for ($j = 0; $j no_fields; $j ++)
        {
            $field_name = $mysql->field[$j];
            echo "Field: ".$field_name."  ----- Value: ".
                $mysql->row[$i][$field_name]."
";
        }
    }

    //==== Use the built-in ShowHTML format
    $status = $mysql->RunDB("select * from user;");
    if ($status != "OK")
    {   echo "


DB Error: $status.
";
        die;
    }
    $mysql->ShowHTML("","","CENTER");

    //==== Run some query not expecting results
    $stmt = ("FILL IN YOUR STAEMENT eg. INSERT INTO");
    $status = $myql->RunDB($stmt, 0);
    if ($status
    if ($status != "OK")
    {   echo "


DB Fail: $status.
";
        die;
    }
    else
    {   echo "
Success: $status.
";
        die;
    }

?>


 



本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

MySQL:世界上最受欢迎的数据库的简介 MySQL:世界上最受欢迎的数据库的简介 Apr 12, 2025 am 12:18 AM

MySQL是一种开源的关系型数据库管理系统,主要用于快速、可靠地存储和检索数据。其工作原理包括客户端请求、查询解析、执行查询和返回结果。使用示例包括创建表、插入和查询数据,以及高级功能如JOIN操作。常见错误涉及SQL语法、数据类型和权限问题,优化建议包括使用索引、优化查询和分表分区。

MySQL的位置:数据库和编程 MySQL的位置:数据库和编程 Apr 13, 2025 am 12:18 AM

MySQL在数据库和编程中的地位非常重要,它是一个开源的关系型数据库管理系统,广泛应用于各种应用场景。1)MySQL提供高效的数据存储、组织和检索功能,支持Web、移动和企业级系统。2)它使用客户端-服务器架构,支持多种存储引擎和索引优化。3)基本用法包括创建表和插入数据,高级用法涉及多表JOIN和复杂查询。4)常见问题如SQL语法错误和性能问题可以通过EXPLAIN命令和慢查询日志调试。5)性能优化方法包括合理使用索引、优化查询和使用缓存,最佳实践包括使用事务和PreparedStatemen

为什么要使用mysql?利益和优势 为什么要使用mysql?利益和优势 Apr 12, 2025 am 12:17 AM

选择MySQL的原因是其性能、可靠性、易用性和社区支持。1.MySQL提供高效的数据存储和检索功能,支持多种数据类型和高级查询操作。2.采用客户端-服务器架构和多种存储引擎,支持事务和查询优化。3.易于使用,支持多种操作系统和编程语言。4.拥有强大的社区支持,提供丰富的资源和解决方案。

apache怎么连接数据库 apache怎么连接数据库 Apr 13, 2025 pm 01:03 PM

Apache 连接数据库需要以下步骤:安装数据库驱动程序。配置 web.xml 文件以创建连接池。创建 JDBC 数据源,指定连接设置。从 Java 代码中使用 JDBC API 访问数据库,包括获取连接、创建语句、绑定参数、执行查询或更新以及处理结果。

docker怎么启动mysql docker怎么启动mysql Apr 15, 2025 pm 12:09 PM

在 Docker 中启动 MySQL 的过程包含以下步骤:拉取 MySQL 镜像创建并启动容器,设置根用户密码并映射端口验证连接创建数据库和用户授予对数据库的所有权限

MySQL的角色:Web应用程序中的数据库 MySQL的角色:Web应用程序中的数据库 Apr 17, 2025 am 12:23 AM

MySQL在Web应用中的主要作用是存储和管理数据。1.MySQL高效处理用户信息、产品目录和交易记录等数据。2.通过SQL查询,开发者能从数据库提取信息生成动态内容。3.MySQL基于客户端-服务器模型工作,确保查询速度可接受。

laravel入门实例 laravel入门实例 Apr 18, 2025 pm 12:45 PM

Laravel 是一款 PHP 框架,用于轻松构建 Web 应用程序。它提供一系列强大的功能,包括:安装: 使用 Composer 全局安装 Laravel CLI,并在项目目录中创建应用程序。路由: 在 routes/web.php 中定义 URL 和处理函数之间的关系。视图: 在 resources/views 中创建视图以呈现应用程序的界面。数据库集成: 提供与 MySQL 等数据库的开箱即用集成,并使用迁移来创建和修改表。模型和控制器: 模型表示数据库实体,控制器处理 HTTP 请求。

centos7如何安装mysql centos7如何安装mysql Apr 14, 2025 pm 08:30 PM

优雅安装 MySQL 的关键在于添加 MySQL 官方仓库。具体步骤如下:下载 MySQL 官方 GPG 密钥,防止钓鱼攻击。添加 MySQL 仓库文件:rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm更新 yum 仓库缓存:yum update安装 MySQL:yum install mysql-server启动 MySQL 服务:systemctl start mysqld设置开机自启动

See all articles