php物件導向思想設計Mysql資料庫操作類

來源:互聯網
上載者:User
host=!empty($config['host'])? $config['host']:"localhost";      $this->port=!empty($config['port'])? $config['port']:"3306";      $this->user=!empty($config['user'])? $config['user']:"root";      $this->password=!empty($config['password'])? $config['password']:"";      $this->charset=!empty($config['charset'])? $config['charset']:"utf8";      $this->dbname=!empty($config['dbname'])? $config['dbname']:"";      $this->link= mysql_connect      ("{$this->host}:{$this->port}", "{$this->user}","{$this->password}")      or die('執行失敗');      $this->setCharset($this->charset);      $this->useDBname($this->dbname);   }   //單例設計模式   static $temp=null;   static function getClass(){      if(!isset(self::$temp)){         $class=new self();      }else{         return self::$temp;      }   }   //可以設定編碼   function setCharset($charset){      mysql_query("set names $charset");   }   //可以設定要串連的資料庫   function useDBname($dbname){      mysql_query("use $dbname");   }   //可以關閉資料庫   function closeDB(){      mysql_close($this->link);      echo"關閉成功!";   }   //執行一條增刪改語句,返回真假結果   function exec($sql){      $result=$this->query($sql);         return true;      }   //返回一行的查詢資料   function gerRow($sql){      $result=$this->query($sql);         $array=mysql_fetch_array($result);      mysql_free_result($result);//釋放資源         return $array;      }   //返回多行的查詢資料   function getRows($sql){      $result=$this->query($sql);         $arr=array();         while($array=mysql_fetch_array($result)){            $arr[]=$array;         }         mysql_free_result($result);//釋放資源         return $arr;      }   //返回一個資料的語句,可以返回一個直接值   function getOneData($sql){      $result=$this->query($sql);      $array=mysql_fetch_array($result);      $data=$array[0];      mysql_free_result($result);//釋放資源      return $data;   }   //可以執行任何sql語句,並進行錯誤處理,或返回執行結果   function query($sql){      $result=mysql_query($sql,$this->link);      if($result===false){         echo"

執行失敗,請參看如下資訊:"; echo"
錯誤代號:".mysql_errno(); echo"
錯誤資訊:".mysql_error(); echo"
錯誤語句:".$sql; die(); }else{ return $result; } }}

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.