qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          一個簡單的封裝數據庫類

          <?php
          class opmysql{
          private $host = 'localhost';            //服務器地址
          private $name = 'root';                 //登錄賬號
          private $pwd = '';                  //登錄密碼
          private $dBase = '';            //數據庫名稱
          private $conn = '';                     //數據庫鏈接資源
          private $result = '';                   //結果集
          private $msg = '';                      //返回結果
          private $fields;                        //返回字段
          private $fieldsNum = 0;                 //返回字段數
          private $rowsNum = 0;                   //返回結果數
          private $rowsRst = '';                  //返回單條記錄的字段數組
          private $filesArray = array();          //返回字段數組
          private $rowsArray = array();           //返回結果數組
          //初始化類
          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();
          }
          //鏈接數據庫
          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");
          }
          //查詢結果
          function mysql_query_rst($sql){
          if($this->conn == ''){
          $this->init_conn();
          }
          $this->result = @mysql_query($sql,$this->conn);
          }
          //取得字段數
          function getFieldsNum($sql){
          $this->mysql_query_rst($sql);
          $this->fieldsNum = @mysql_num_fields($this->result);
          }
          //取得查詢結果數
          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 getFields($sql,$fields){
          $this->mysql_query_rst($sql);
          if(mysql_errno() == 0){
          if(mysql_num_rows($this->result) > 0){
          $tmpfld = @mysql_fetch_row($this->result);
          $this->fields = $tmpfld[$fields];
          }
          return $this->fields;
          }else{
          return '';
          }
          }
          //錯誤信息
          function msg_error(){
          if(mysql_errno() != 0) {
          $this->msg = mysql_error();
          }
          return $this->msg;
          }
          //釋放結果集
          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 = '';
          }
          }
          $conn = new opmysql();
          ?>

          posted on 2014-04-24 10:30 順其自然EVO 閱讀(258) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 高邑县| 宣恩县| 正宁县| 石台县| 昂仁县| 老河口市| 平度市| 永靖县| 盱眙县| 乐陵市| 文山县| 台东市| 迁西县| 石家庄市| 溧水县| 治县。| 饶河县| 山阴县| 元谋县| 凤凰县| 宝山区| 台南县| 广东省| 娄底市| 枞阳县| 当雄县| 孟州市| 吉木乃县| 贵港市| 宁晋县| 陆良县| 绥化市| 滨州市| 绵阳市| 芷江| 故城县| 额济纳旗| 高阳县| 木兰县| 东光县| 金华市|