mysql 串連類

來源:互聯網
上載者:User
<?

class Mysql{
           var $linkid;
           var $db_host;
           var $db_user;
           var $db_pwd;
           var $db_name;
           var $querynum=0;
           var $selectid;
           var $result= null;
           var $pconnect = true;

//建構函式,初始化類時需帶入資料庫參數
function Mysql($db_host,$db_user,$db_pwd,$db_name,$pconnect = true){
           $this->db_host =$db_host;
           $this->db_user =$db_user;
           $this->db_pwd =$db_pwd;
           $this->db_name =$db_name;

           if($this->pconnect == $pconnect){
             $this->linkid=@mysql_pconnect($this->db_host,$this->db_user,$this->db_pwd);
             }
             else{
             $this->linkid=@mysql_connect($this->db_host,$this->db_user,$this->db_pwd);
             }

             if($this->linkid){
               if($this->db_name!=""){
                 $dbselect=@mysql_select_db($this->db_name) or $this->mysql_err("database not exists");
                 }
               }
               else{
               $this->Mysql_err("cannot connect the database server,pls chk your password");
               }
           //$this->query("SET NAMES 'utf8'");
           }

//用於執行SQL語句
function Query($sql){
                    $this->result=@mysql_query($sql,$this->linkid) or $this->mysql_err("SQL語句".$sql."錯誤");
                    $this->querynum++;
                    return $this->result;
                    }

function fetch_array($sql) {
   return mysql_fetch_array($sql,MYSQL_ASSOC);
}

//用於統計記錄的數目
function Number($sql){
                     $this->result=$this->query($sql);
                     $number=mysql_num_rows($this->result);
                     $this->Free();
                     return $number;
                     }

//取得一條記錄
function GetRow($sql){
                     $this->query($sql);
                     $row=mysql_fetch_array($this->result,MYSQL_ASSOC);
                     $this->Free();
                     return $row;
                     }
//取得全部記錄
function GetRows($sql){
                      $this->result=$this->query($sql);
                      while($row=mysql_fetch_array($this->result,MYSQL_ASSOC))
                           {
                           $allrows[]=$row;
                           }
                      $this->Free();
                      return $allrows;
                      }

//取得當前插入ID即ID欄位
function InsertID() {
                $id = mysql_insert_id();
                return $id;
                     }
//關閉資料庫
function Close(){
                @mysql_close($this->linkid);
                }
//列印錯誤資訊
function Mysql_err($msg){
                        echo $msg;
                        exit;
                        }
//釋放
function Free(){
               @mysql_free_result($this->result);
               $this->result=null;
               }

}
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.