官术网_书友最值得收藏!

1.3 數據庫設計

1.3.1 數據庫設計概述

注冊及登錄驗證模塊使用的數據庫是db_reglog,庫中包含了一個數據表tb_member。該表中有12個字段。tb_member表結構及說明如圖1.8所示。

圖1.8 tb_member表結構及說明

1.3.2 封裝數據庫連接操作類

為了便于維護,減少代碼冗余,本模塊使用了一個簡單的自定義數據庫類。下面給出類中的主要代碼。

首先給出的是類中的成員變量,其中$host、$name、$pwd和$dBase變量是類初始化時所使用的,有默認值,其他變量都是相關的數據信息,代碼如下:

      <? php
      class opmysql{
            private $host = ' localhost' ;                  //服務器地址
            private $name = ' root' ;                       //登錄賬號
            private $pwd = '111' ;                          //登錄密碼
            private $dBase = ' db_reglog' ;                 //數據庫名稱
            private $conn = ' ' ;                           //數據庫連接資源
            private $result = ' ' ;                         //結果集
            private $msg = ' ' ;                            //返回結果
            private $fields;                                //返回字段
            private $fieldsNum = 0;                         //返回字段數
            private $rowsNum = 0;                           //返回結果數
            private $rowsRst = ' ' ;                        //返回單條記錄的字段數組
            private $filesArray = array();                  //返回字段數組
            private $rowsArray = array();                   //返回結果數組

接下來給出的是構造函數。該構造函數有4個參數,這4個參數都可以被省略而使用默認值。參數處理后,調用init_conn()函數,代碼如下:

            function __construct($host=' ' , $name=' ' , $pwd=' ' , $dBase=' ' ){
                if($host ! = ' ' )
                    $this->host = $host;
                if($name ! = ' ' )
                    $this->name = $name;
                if($pwd ! = ' ' )
                    $this->pwd = $pwd;
                if($dBase ! = ' ' )
                    $this->dBase = $dBase;
                $this->init_conn();
            }

init_conn()函數根據成員變量中的值來創建數據庫連接源,代碼如下:

            //連接數據庫
            function init_conn(){
                $this->conn=@mysql_connect($this->host, $this->name, $this->pwd);
                @mysql_select_db($this->dBase, $this->conn);
                mysql_query("set names gb2312");                   //設置編碼
            }

下面就是類中的操作函數了。先來看查詢函數。查詢函數根據傳過來的SQL語句進行查詢,并將查詢結果保存到成員變量$result中。符號“@”的作用是屏蔽錯誤信息,代碼如下:

            //查詢結果
            function mysql_query_rst($sql){
                if($this->conn == ' ' ){
                  $this->init_conn();
                }
                $this->result = @mysql_query($sql, $this->conn);
            }

返回查詢記錄數函數。根據查詢結果,返回記錄數,代碼如下:

            function getRowsNum($sql){
                $this->mysql_query_rst($sql);
                if(mysql_errno() == 0){
                  return @mysql_num_rows($this->result);
                }else{
                  return ' ' ;
                }
            }

取得記錄數組函數。將查詢結果輸出成一個數組并返回。該函數處理的是單條記錄,代碼如下:

            function getRowsRst($sql){
                $this->mysql_query_rst($sql);
                if(mysql_error() == 0){
                  $this->rowsRst = mysql_fetch_array($this->result, MYSQL_ASSOC);
                  return $this->rowsRst;
                }else{
                  return ' ' ;
                }
            }

取得記錄數組函數。功能同上,只是這里返回一個有多條記錄的二維數組,代碼如下:

            //取得記錄數組(多條記錄)
            function getRowsArray($sql){
                $this->mysql_query_rst($sql);
                if(mysql_errno() == 0){
                    while($row = mysql_fetch_array($this->result, MYSQL_ASSOC)) {
                        $this->rowsArray[] = $row;
                    }
                    return $this->rowsArray;
                }else{
                    return ' ' ;
                }
            }

返回更新、刪除、添加的記錄數函數。該函數用來更新、刪除、添加記錄并獲取受影響的記錄數,用來判斷操作是否成功,代碼如下:

            function uidRst($sql){
                if($this->conn == ' ' ){
                  $this->init_conn();
                }
                @mysql_query($sql);
                $this->rowsNum = @mysql_affected_rows();
                if(mysql_errno() == 0){
                  return $this->rowsNum;
                }else{
                  return ' ' ;
                }
            }

釋放結果集函數。將不再使用的數據刪除,釋放內存,代碼如下:

            function close_rst(){
                mysql_free_result($this->result);
                $this->msg = ' ' ;
                $this->fieldsNum = 0;
                $this->rowsNum = 0;
                $this->filesArray = ' ' ;
                $this->rowsArray = ' ' ;
            }

關閉數據庫函數,代碼如下:

            //關閉數據庫
            function close_conn(){
                $this->close_rst();
                mysql_close($this->conn);
                $this->conn = ' ' ;
            }
      }
      $conne = new opmysql();
      ?>
主站蜘蛛池模板: 安国市| 阜康市| 阳泉市| 长兴县| 通城县| 西乌| 梅州市| 扎赉特旗| 西昌市| 保定市| 二手房| 佳木斯市| 大悟县| 虞城县| 红河县| 临汾市| 武宁县| 两当县| 临洮县| 行唐县| 温泉县| 合肥市| 临沭县| 当涂县| 洪泽县| 天水市| 祁阳县| 崇左市| 阿尔山市| 阳泉市| 谷城县| 渝中区| 伊宁县| 兴业县| 中牟县| 叙永县| 吴旗县| 新竹市| 盐城市| 普兰县| 毕节市|