首页 > 数据库 > MySQL > 正文

php- mysql连接类实现

2024-07-24 12:56:36
字体:
来源:转载
供稿:网友
菜鸟学堂:
  • /*************************************
        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->[email protected]_pconnect($this->dbserver,$this->dbuser,$this->dbpwd);//long connect
       else
        $this->[email protected]_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">[email protected]_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;$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);
       $[email protected]_query("select count(".$index.") as 'num' form".$table."where".$where,$this->dblink);
       if(!$result) $this->dbhalt("wrong sql sentence".$sql);
        $[email protected]_result($result,0,"num");
       return $num;
      }
      function dbgetnum($result){
       $[email protected]_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";

     


    ?>

  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表