php- mysql连接类实现

80酷酷网    80kuku.com

  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())
        $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;$i<$this->numrows;$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);
        count(".$index.") as 'num' form".$table."where".$where,$this->dbLink);
       if(!$result) $this->dbhalt("wrong sql sentence".$sql);
        ");
       return $num;
      }
      function dbgetnum($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";

    ?>



  • 分享到
    • 微信分享
    • 新浪微博
    • QQ好友
    • QQ空间
    点击: