找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 24|回复: 0

[PHP] php MySQL数据库操作类源代码

[复制链接]

1883

主题

520

回帖

7366

积分

管理员

积分
7366
发表于 2024-9-7 14:51:02 | 显示全部楼层 |阅读模式
  1. <?php
  2. class MySQL{
  3.     private $host;                            //服务器地址
  4.     private $name;                            //登录账号
  5.     private $pwd;                            //登录密码
  6.     private $dBase;                            //数据库名称
  7.     private $conn;                            //数据库链接资源
  8.     private $result;                        //结果集
  9.     private $msg;                            //返回结果
  10.     private $fields;                        //返回字段
  11.     private $fieldsNum;                        //返回字段数
  12.     private $rowsNum;                        //返回结果数
  13.     private $rowsRst;                        //返回单条记录的字段数组
  14.     private $filesArray = array();            //返回字段数组
  15.     private $rowsArray = array();            //返回结果数组
  16.     private $charset='utf8';                //设置操作的字符集
  17.     private $query_count=0;                 //查询结果次数
  18.     static private $_instance;                 //存储对象
  19.     //初始化类
  20.     private function __construct($host='',$name='',$pwd='',$dBase=''){
  21.         if($host  !=  '')  $this->host  = $host;
  22.         if($name  !=  '')  $this->name  = $name;
  23.         if($pwd   !=  '')  $this->pwd   = $pwd;
  24.         if($dBase !=  '')  $this->dBase = $dBase;
  25.         $this->init_conn();
  26.     }
  27.     //防止被克隆
  28.     private function __clone(){}
  29.     public static function getInstance($host='',$name='',$pwd='',$dBase=''){
  30.         if(FALSE == (self::$_instance instanceof self)){
  31.             self::$_instance = new self($host,$name,$pwd,$dBase);
  32.         }
  33.         return self::$_instance;
  34.     }
  35.     public function __set($name,$value){
  36.         $this->$name=$value;
  37.     }
  38.     public function __get($name){
  39.         return $this->$name;
  40.     }
  41.     //链接数据库
  42.     function init_conn(){
  43.         $this->conn=@mysql_connect($this->host,$this->name,$this->pwd) or die('connect db fail !');
  44.         @mysql_select_db($this->dBase,$this->conn) or die('select db fail !');
  45.         mysql_query("set names ".$this->charset);
  46.     }
  47.     //查询结果
  48.     function mysql_query_rst($sql){
  49.         if($this->conn == '') $this->init_conn();
  50.         $this->result = @mysql_query($sql,$this->conn);
  51.         $this->query_count++;
  52.     }
  53.     //取得字段数
  54.     function getFieldsNum($sql){
  55.         $this->mysql_query_rst($sql);
  56.         $this->fieldsNum = @mysql_num_fields($this->result);
  57.     }
  58.     //取得查询结果数
  59.     function getRowsNum($sql){
  60.         $this->mysql_query_rst($sql);
  61.         if(mysql_errno() == 0){
  62.             return @mysql_num_rows($this->result);
  63.         }else{
  64.             return '';
  65.         }   
  66.     }
  67.     //取得记录数组(单条记录)
  68.     function getRowsRst($sql,$type=MYSQL_BOTH){
  69.         $this->mysql_query_rst($sql);
  70.         if(empty($this->result)) return '';
  71.         if(mysql_error() == 0){
  72.             $this->rowsRst = mysql_fetch_array($this->result,$type);
  73.             return $this->rowsRst;
  74.         }else{
  75.             return '';
  76.         }
  77.     }
  78.     //取得记录数组(多条记录)
  79.     function getRowsArray($sql,$type=MYSQL_BOTH){
  80.         !empty($this->rowsArray) ? $this->rowsArray=array() : '';
  81.         $this->mysql_query_rst($sql);
  82.         if(mysql_errno() == 0){
  83.             while($row = mysql_fetch_array($this->result,$type)) {
  84.                 $this->rowsArray[] = $row;
  85.             }
  86.             return $this->rowsArray;
  87.         }else{
  88.             return '';
  89.         }
  90.     }
  91.     //更新、删除、添加记录数
  92.     function uidRst($sql){
  93.         if($this->conn == ''){
  94.             $this->init_conn();
  95.         }
  96.         @mysql_query($sql);
  97.         $this->rowsNum = @mysql_affected_rows();
  98.         if(mysql_errno() == 0){
  99.             return $this->rowsNum;
  100.         }else{
  101.             return '';
  102.         }
  103.     }
  104.     //返回最近插入的一条数据库的id值
  105.     function returnRstId($sql){
  106.         if($this->conn == ''){
  107.             $this->init_conn();
  108.         }
  109.         @mysql_query($sql);
  110.         if(mysql_errno() == 0){
  111.             return mysql_insert_id();
  112.         }else{
  113.             return '';
  114.         }
  115.     }
  116.     //获取对应的字段值
  117.     function getFields($sql,$fields){
  118.         $this->mysql_query_rst($sql);
  119.         if(mysql_errno() == 0){
  120.             if(mysql_num_rows($this->result) > 0){
  121.                 $tmpfld = @mysql_fetch_row($this->result);
  122.                 $this->fields = $tmpfld[$fields];
  123.                
  124.             }
  125.             return $this->fields;
  126.         }else{
  127.             return '';
  128.         }
  129.     }
  130.     //错误信息
  131.     function msg_error(){
  132.         if(mysql_errno() != 0) {
  133.             $this->msg = mysql_error();
  134.         }
  135.         return $this->msg;
  136.     }
  137.     //释放结果集
  138.     function close_rst(){
  139.         mysql_free_result($this->result);
  140.         $this->msg = '';
  141.         $this->fieldsNum = 0;
  142.         $this->rowsNum = 0;
  143.         $this->filesArray = '';
  144.         $this->rowsArray = '';
  145.     }
  146.     //关闭数据库
  147.     function close_conn(){
  148.         $this->close_rst();
  149.         mysql_close($this->conn);
  150.         $this->conn = '';
  151.     }
  152.     //取得数据库版本
  153.     function db_version() {
  154.         return mysql_get_server_info();
  155.     }
  156. }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表