仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2227|回复: 19
打印 上一主题 下一主题

[学习教程] PHP网页编程之一介简介的Mysql封装类

[复制链接]
山那边是海 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-16 00:27:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
让好朋友来看看,嘿,看咱写的多棒,然后再在网上宣传一下。mysql|封装   class_mysql.php
<?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 < $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=" ";
                printf("<TD>%s", $f_value);
            }
        }
        //--- THE END
        echo "</TABLE>";
    } // ShowHTML
} // end class MYSQL
?>
例子:
<?php
    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 "<HR>DB Error: $status.<HR>";
        die;
    }
    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>";
        }
    }
    //==== Use the built-in ShowHTML format
    $status = $mysql->RunDB("select * from user;");
    if ($status != "OK")
    {   echo "<HR>DB Error: $status.<HR>";
        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 "<HR>DB Fail: $status.<HR>";
        die;
    }
    else
    {   echo "<HR>Success: $status.<HR>";
        die;
    }
?>


总的来说,在这一个月左右的时间中,学到的不少,但是也遇到不少的问题,比如批量图片的上传,一直到现在也不懂,如何实现动态的增加上传图片的数量。
admin 该用户已被删除
沙发
发表于 2015-2-16 02:58:39 | 只看该作者
爱上php,他也会爱上你。
金色的骷髅 该用户已被删除
板凳
发表于 2015-2-27 21:11:13 | 只看该作者
我还是推荐用firefox ,配上firebug 插件调试js能省下不受时间。谷歌的浏览器最好也不少用,因为谷歌的大侠们实在是太天才啦,把一些原来的js代码加了一些特效。
山那边是海 该用户已被删除
地板
 楼主| 发表于 2015-3-6 05:18:59 | 只看该作者
当留言板完成的时候,下步可以把做1个单人的blog程序,做为目标,
莫相离 该用户已被删除
5#
发表于 2015-3-7 19:05:45 | 只看该作者
遇到出错的时候,我经常把错误信息直接复制到 google的搜索栏,一般情况都是能搜到结果的,不过有时候会搜出来一大片英文的出来,这时候就得过滤一下,吧中文的弄出来,挨着式方法。
飘灵儿 该用户已被删除
6#
发表于 2015-3-15 12:07:00 | 只看该作者
做为1门年轻的语言,php一直很努力。
简单生活 该用户已被删除
7#
发表于 2015-3-22 00:26:23 | 只看该作者
我学习了一段时间后,我发现效果并不好(估计是我自身的问题)。因为一个人的精力总是有限的,同时学习这么多,会导致每个的学习时间都得不到保证。
再现理想 该用户已被删除
8#
发表于 2015-3-23 21:15:25 | 只看该作者
做为1门年轻的语言,php一直很努力。
分手快乐 该用户已被删除
9#
发表于 2015-3-27 01:10:51 | 只看该作者
建议加几个专业的phper的群,当然啦需要说话的人多,一处一点问题能有人回答你的,当然啦要让人回答你的问题,平时就得躲在里面聊天,大家混熟啦,愿意回答你问题的人自然就多啦。
透明 该用户已被删除
10#
发表于 2015-4-4 19:40:20 | 只看该作者
在我安装pear包的时候老是提示,缺少某某文件,才发现 那群extension 的排列是应该有一点的顺序,而我安装的版本的排序不是正常的排序。没办法我只好把那群冒号加了上去,只留下我需要使用的扩展。
深爱那片海 该用户已被删除
11#
发表于 2015-4-11 09:16:29 | 只看该作者
建数据库表的时候,int型要输入长度的,其实是个摆设的输入几位都没影响的,只要大于4就行,囧。
老尸 该用户已被删除
12#
发表于 2015-4-15 20:08:24 | 只看该作者
学好程序语言,多些才是王道,写两个小时代码的作用绝对超过看一天书,这个我是深有体会(顺便还能练打字速度)。
活着的死人 该用户已被删除
13#
发表于 2015-4-16 15:09:10 | 只看该作者
本人接触php时间不长,算是phper中的小菜鸟一只吧。由于刚开始学的时候没有名师指,碰过不少疙瘩,呗很多小问题卡过很久,白白浪费不少宝贵的时间,在次分享一些子的学习的心得。
灵魂腐蚀 该用户已被删除
14#
发表于 2015-4-27 21:23:45 | 只看该作者
对于懒惰的朋友,我推荐php的集成环境xampp或者是wamp。这两个软件安装方便,使用简单。但是我还是强烈建议自己动手搭建开发环境。
飘飘悠悠 该用户已被删除
15#
发表于 2015-4-28 22:40:51 | 只看该作者
先学习php和mysql,还有css(html语言很简单)我认为现在的效果比以前的方法好。
只想知道 该用户已被删除
16#
发表于 2015-5-9 05:30:43 | 只看该作者
对于初学者来说不推荐去拿钱买的。当然如果一个网站你经常去用,而且里面的资料也比较有用,最好还是买个会员比较好,毕竟那些也是别人的工作成果。
若天明 该用户已被删除
17#
发表于 2015-6-17 15:31:32 | 只看该作者
没接触过框架的人,也不用害怕,其实框架就是一种命名规范及插件,学会一个框架其余的框架都很好上手的。
小魔女 该用户已被删除
18#
发表于 2015-6-29 11:37:21 | 只看该作者
写js我最烦的就是 ie 和 firefox下同样的代码 结果显示的结果千差万别,还是就是最好不要用遨游去调试,因为有时候遨游是禁用js的,有可能代码是争取结果被遨游折腾的认为是代码写错。
小女巫 该用户已被删除
19#
发表于 2015-7-7 16:41:22 | 只看该作者
开发工具也会慢慢的更专业,每个公司的可能不一样,但是zend studio是个大伙都会用的。
兰色精灵 该用户已被删除
20#
发表于 2015-7-21 21:40:44 | 只看该作者
php是动态网站开发的优秀语言,在学习的时候万万不能冒进。在系统的学习前,我认为不应该只是追求实现某种效果,因为即使你复制他人的代码调试成功,实现了你所期望的效果,你也不了解其中的原理。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-7 12:04

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表