Heim > php教程 > php手册 > php- mysql连接类实现

php- mysql连接类实现

WBOY
Freigeben: 2016-06-21 09:05:56
Original
935 Leute haben es durchsucht

mysql

  • /*************************************
        mysql连接类实现 -write by 三千
     **************************************/
     /*
     使用方法(可以执行一般mysql命令,insert, delete select  update)
     在文件前,需要加载方法
     require("mysql.class.php")
     $db=new dbMysql; 加载类
     $db->dbServer="localhost";
     $db->dbUser="root";
     $db->dbPwd="";
     $db->dbDatabase="";
     $db->dbConnect();

     query($sql,$database);
     query_first($sql,$database);
     fetch_array($sql,$database);
     count_records($table,$index,$where,$database);)//为得到一个表记录的数目,$table为表名,$index为key,$where为条件,$dbbase为数据库,后两个可以不选
     */

     class db_Mysql
     {
      
      var $usepconnect;
      var $dbSever;
      var $dbDatabase;
      var $dbbase;
      var $dbUser;
      var $dbPwd;
      var $dbLink;
      var $query_id;// 执行query命令的指针
      var $num_rows;// 返回的条目数
      var $insert_id;// 传回最后一次使用 INSERT 指令的 ID
      var $affected_rows;// 传回query命令所影响的列数目
                      // INSERT、UPDATE 或 DELETE 所影响的列 (row) 数目。
                      // delete 如果不带where,那么则返回0
      function dbconnect($dbbase,$usepconnect)
      {
       //global $usepconnect;
       if($usepconnect==1)
        $this->dbLink=@mysql_pconnect($this->dbServer,$this->dbUser,$this->dbPwd);//long connect
       else
        $this->dbLink=@mysql_connect($this->dbServer,$this->dbUser,$this->dbPwd);//short connect
       //$this->dbhalt($this->dbbase);
       if(!$this->dbLink) $this->dbhalt("exsiting error when connecting!");
       if($this->dbbase=="") $this->dbbase=$this->dbDatabase;
       if(dbbase,$this->dbLink">!@mysql_select_db($this->dbbase,$this->dbLink))
        $this->dbhalt("can't use this database,please check database!");
      } // end func() connect database
      //change database
      function dbchange_db($dbbase){
       $this->dbconnect($dbbase);
      
      }
      function dbquery($sql){
       //if($dbbase!="") $this->dbchange_db($dbbase);
       $this->query_id=mysql_query($sql);
       //print($query_id);
       if(!$this->query_id) $this->dbhalt("wrong sql sentence!".$sql);
       return $this->query_id;
      }
      function dbquery_first($sql,$dbbase){
       $query_id=dbquery($sql,$dbbase);
       $returnarray=mysql_fetch_array($query_id);
       $this->num_rows=mysql_num_rows($query_id);
       $this->dbfree_result($query_id);
       return $returnarray;
      }
      function dbhalt($errmsg){
       $msg="

    database is wrong!


    ";
       $msg=$errmsg;
       echo"$msg";
       die();
      }
      function dbfetch_array($sql,$dbbase,$type){
       $query_id=$this->dbquery($sql,$dbbase);
       $this->numrows=mysql_num_rows($query_id);
       for($i=0;$inumrows;$i++){
        if($type==0)
         $array[$i]=mysql_fetch_array($query_id);
        else
         $array[$i]=mysql_fetch_array($query_id);
       }
       $this->dbfree_result($query_id);
       return $array;
      }
      function dbdelete($sql,$dbdase){
       $query_id=$this->dbquery($sql,$dbbase);
       $this->affected_rows=mysql_affected_rows($this->dbLink);
       $this->free_reuslt($query_id);
      }
      function dbinsert($sql,$dbbase){
       $query_id=$this->dbquery($sql,$dbbase);
       $this->insert_id=mysql_insert_id($this->dbLink);
       $this->affected_rows=mysql_affected_rows($this->dbLink);
       $this->free_reuslt($query_id);
      }
      function dbupdate($sql,$dbbase){
       $query_id=$this->dbquery($sql,$dbbase);
       $this->insert_id=mysql_insert_id($this->dbLink);
       $this->affected_rows=mysql_affected_rows($this->dbLink);
       $this->free_reuslt($query_id);
      }
      function dbcount_records($table,$index="id",$where="",$dbbase){
       if($dbbase!="")$this->dbchangedb($dbbase);
       $result=@mysql_query("select count(".$index.") as 'num' form".$table."where".$where,$this->dbLink);
       if(!$result) $this->dbhalt("wrong sql sentence".$sql);
        $num=@mysql_result($result,0,"num");
       return $num;
      }
      function dbgetnum($result){
       $num=@mysql_numrows($result);
       return $num;
      }
      function dbfree_result($result_id){
       @mysql_free_result($query_id);
      }
      function dbclose(){
       mysql_close($this->dbLink);
      }
     } // end class
     $db=new db_Mysql;
     $db->dbServer="localhost";
     $db->dbUser="root";
     $db->dbPwd="";
     $db->dbbase="test";


    ?>



  • Verwandte Etiketten:
    Quelle:php.cn
    Erklärung dieser Website
    Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
    Beliebte Empfehlungen
    Beliebte Tutorials
    Mehr>
    Neueste Downloads
    Mehr>
    Web-Effekte
    Quellcode der Website
    Website-Materialien
    Frontend-Vorlage