最近写的一个MySQL操作类(含调用方法),发上来与大家共享

请大家提出宝贵意见和建议,谢谢!
<?php
 /*
 *  @filename:mysql.class.php
 *  @author:forest
 *  @email:chaizuxue@163.com
 *  @create:2006-10-24
 *  @environment:PHP4 or PHP5
 *  @copyright:Copyright 2006 forest
 *  
 -- 数据库: `test`
 --

 -- --------------------------------------------------------

 --
 -- 表的结构 `administrator`
 --

 CREATE TABLE `administrator` (
  `id` int(11) NOT NULL auto_increment,
 `username` varchar(50) NOT NULL default '',
 `password` varchar(50) NOT NULL default '',
 `groupid` int(11) NOT NULL default '0',
 PRIMARY KEY  (`id`)
 ) TYPE=MyISAM COMMENT='后台管理' AUTO_INCREMENT=5 ;

 --
 -- 导出表中的数据 `administrator`
 --

 INSERT INTO `administrator` VALUES (1, 'test', 'test', 1);
 INSERT INTO `administrator` VALUES (2, 'administratorer', 'administratorer', 1);
 INSERT INTO `administrator` VALUES (3, 'administratorer', 'administratorer', 1);
 INSERT INTO `administrator` VALUES (4, 'user', '123456', 0);

 *    Example:
   $mysql=new mysql("localhost","root","root","test");
   $sql="SELECT * FROM administrator";
   $result=$mysql->execute($sql);
   print_r($result);
   $mysql->close($mysql->link);
 */
 class mysql
 {
   var $hostname,$username,$password,$database,$result;
   var $pconnect=false;
   var $debug=false;
   var $link;
   var $record=array();
   var $error,$errno;

   function mysql($hostname,$username,$password,$database)
   {
     $this->set("hostname",$hostname);
     $this->set("username",$username);
     $this->set("password",$password);
     $this->set("database",$database);
     $this->connect_db();
     $this->select_db();
   }
   
   function __construct($hostname="localhost",$username,$password,$database)
   {
     $this->set("hostname",$hostname);
     $this->set("username",$username);
     $this->set("password",$password);
     $this->set("database",$database);
     $this->connect_db();
     $this->select_db();
   }
   
   function set($var,$value)
   {
     $this->$var=$value;
   }
   
   function connect_db($hostname="",$username="",$password="")
   {
     if(!empty($hostname))  $this->set("hostname",$hostname);
     if(!empty($username))   $this->set("username",$username);
     if(!empty($password))   $this->set("password",$password);
     if(!$this->link)
     {
       if($this->pconnect)
         $this->link=mysql_pconnect($this->hostname,$this->username,$this->password);
       else
         $this->link=mysql_connect($this->hostname,$this->username,$this->password);
     }
     else die("Mysql Connect Error in ".__FUNCTION__."():".mysql_errno().":".mysql_error());
     return $this->link;
   }
   
   function select_db()
   {
     mysql_select_db($this->database,$this->link) or die(mysql_error());
   }
   //------------------------------------------------------------------------------------  


   //------------------------------------------------------------------------------------  
   /*调用方法:
   include "mysql.class.php";
   $mysql=new mysql("localhost","root","root","test");
   $sql="SELECT * FROM `administrator`";
   $rel=$mysql->query($sql);
   $mysql->free_result();
   */
   function query($sql)
   {
     if($this->result=@mysql_query($sql,$this->link))
       return $this->result;
     else      
     {
       echo "<center><br>SQL语句错误:<font color=red>$sql</font><br>错误信息:".mysql_error();
       return false;
     }
   }
   //------------------------------------------------------------------------------------


   /*------------------------------------------------------------------------------------
   /*调用方法:
   include "mysql.class.php";
   $mysql=new mysql("localhost","root","root","test");
   $sql="SELECT * FROM `administrator`";
   $rel=$mysql->execute($sql);
   print_r($rel);
   $mysql->free_result();
   */
   function execute($sql,$style="array")
   {
     $this->query($sql);
     if(!empty($this->record))
       $this->record=array();
     $i=0;
     if($style=="array")
     {
       while ($temp=@mysql_fetch_array($this->result))
       {
         $this->record[$i]=$temp;
         $i++;
       }
     }
     else
     {
       while ($temp=@mysql_fetch_object($this->result))
       {
         $this->record[$i]=$temp;
         $i++;
       }          
     }
     unset($i);
     unset($temp);
     return $this->record;
   }
   //------------------------------------------------------------------------------------


   //------------------------------------------------------------------------------------
   //*功能描述:返回根据所取得的行生成的对象
   /*调用方法:
   include "mysql.class.php";
   $mysql=new mysql("localhost","root","root","test");
   $sql="SELECT * FROM `administrator`";
   $obj=$mysql->fetch_object($sql);
   echo $obj->username;
   $mysql->free_result();
   */
   function fetch_object($sql)
   {
     $this->result=$this->query($sql);
     while ($obj=mysql_fetch_object($this->result))
     {
       $object=$obj;  
     }
     return $object;
   }
   //------------------------------------------------------------------------------------

   
   //------------------------------------------------------------------------------------
   //*功能描述:返回最后一条记录集
   /*调用方法:
   include "mysql.class.php";
   $mysql=new mysql("localhost","root","root","test");
   $sql="SELECT * FROM `administrator`";
   $array=$mysql->fetch_one_array($sql);
   print_r($array);
   $mysql->free_result();
   */
   function fetch_one_array($sql)
   {
     return @mysql_fetch_array($this->query($sql));
   }
   //------------------------------------------------------------------------------------
   

   //------------------------------------------------------------------------------------
   //*功能描述:返回最后一条记录集
   /*调用方法:
   include "mysql.class.php";
   $mysql=new mysql("localhost","root","root","test");
   $sql="SELECT * FROM `administrator`";
   $array=$mysql->fetch_array($sql);
   print_r($array);
   $mysql->free_result();
   */
   function fetch_array($sql)
   {
     $this->result=@mysql_query($sql);
     while ($row=@mysql_fetch_array($this->result))
     {
       $array[]=$row;  
     }
     return $array;
   }
   //------------------------------------------------------------------------------------


 //------------------------------------------------------------------------------------  
   //*功能描述:取得最近一次与link关联的INSERT,UPDATE或DELETE查询所影响的记录行数
   /*调用方法:
   include "mysql.class.php";
   $mysql=new mysql("localhost","root","root","test");
   $sql="SELECT * FROM `administrator`";
   $rel=$mysql->fetch_array($sql);
   echo $mysql->affected_rows($mysql->link);
   */
   function affected_rows()
   {
     return mysql_affected_rows($this->link);
   }
 //------------------------------------------------------------------------------------


 //------------------------------------------------------------------------------------  
   //*功能描述:取得数据库中记录总数
   /*调用方法:
   include "mysql.class.php";
   $mysql=new mysql("localhost","root","root","test");
   $sql="SELECT * FROM `administrator`";
   echo $mysql->num_rows($sql);
   */
   function num_rows($sql)
   {
     return @mysql_num_rows($this->query($sql));
   }
 //------------------------------------------------------------------------------------

 
 //------------------------------------------------------------------------------------
   //*功能描述:返回结果集中字段的数目
   /*调用方法:
   include"mysql.class.php";
   $mysql=new mysql("localhost","root","root","test");
   $sql="SELECT * FROM `administrator`";
   echo $mysql->num_fields($sql);
   */
   function num_fields($sql)
   {
     return @mysql_num_fields($this->query($sql));
   }
 //------------------------------------------------------------------------------------
   //*功能描述:取得上一步INSERT操作产生的ID
   /*调用方法:
   include"mysql.class.php";
   $mysql=new mysql("localhost","root","root","test");
   $sql="INSERT INTO `administrator` VALUES ('', 'test', 'test', 0)";
   $mysql->query($sql);
   echo $mysql->insert_id();
   */
   function insert_id()
   {
     return mysql_insert_id($this->link);
   }
 //------------------------------------------------------------------------------------


 //------------------------------------------------------------------------------------
   //*功能描述:取得MySQL客户端信息
   /*调用方法:
   include"mysql.class.php";
   $mysql=new mysql("localhost","root","root","test");
   echo $mysql->get_client_info();
   */
   function get_client_info()
   {
     return mysql_get_client_info();
   }
 //------------------------------------------------------------------------------------
 

 //------------------------------------------------------------------------------------
   //*功能描述:取得MySQL主机信息
   /*调用方法:
   include"mysql.class.php";
   $mysql=new mysql("localhost","root","root","test");
   echo $mysql->get_host_info();
   */
   function get_host_info()
   {
     return mysql_get_host_info();
   }
 //------------------------------------------------------------------------------------
 

 //------------------------------------------------------------------------------------
   //*功能描述:取得MySQL协议信息

   /*调用方法:
   include"mysql.class.php";
   $mysql=new mysql("localhost","root","root","test");
   echo $mysql->get_proto_info();
   */
   function get_proto_info()
   {
     return mysql_get_proto_info();
   }
 //------------------------------------------------------------------------------------
   //*功能描述:取得MySQL服务器信息
   /*调用方法:
   include"mysql.class.php";
   $mysql=new mysql("localhost","root","root","test");
   echo $mysql->get_server_info();
   */
   function get_server_info()
   {
     return mysql_get_server_info();
   }
 //------------------------------------------------------------------------------------  


 //------------------------------------------------------------------------------------
   //*功能描述:取得数据库中记录总数
   /*调用方法:
   include"mysql.class.php";
   $mysql=new mysql("localhost","root","root","test");
   $sql="SELECT * FROM `administrator`";
   $rel=$mysql->execute($sql);
   $mysql->free_result();
   unset($rel);
   print_r($rel);
   print_r($mysql->result);
   */
   function free_result()
   {
     return is_resource($this->result) ? mysql_free_result($this->result) : true;
   }
   
   function get_error()
   {
     $this->error=mysql_error($this->link);
     return $this->error;
   }
   
   function get_errno()
   {
     $this->errno=mysql_errno($this->link);
     return $this->errno;
   }
   
   function close_db()
   {
     $this->free_result();
     mysql_close($this->link) or die(mysql_error());
   }
   
   function __destruct()
   {
     $this->close_db();
     $this->set("hostname","");
     $this->set("username","");
     $this->set("password","");
     $this->set("database","");
   }
   
   function insert($table,$field)
   {
     $temp=explode(',',$field);
     $insert='';
     for($i=0;$i<count($temp);$i++)
     {
       $insert.="'".$_POST[$temp[$i]]."',";
     }
     $insert=substr($insert,0,-1);
     $sql="INSERT INTO ".$table." (".$field.") VALUES (".$insert.")";
     $this->query($sql);
   }
   
   function update($table,$set,$condition)
   {
     $sql="UPDATE ".$table." SET ".$set." WHERE ".$condition;
     $this->query($sql);
   }
   
   function delete($table,$condition)
   {
     $sql="DELETE FROM ".$table." WHERE ".$condition;
     $this->query($sql);
   }
 }
?>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值