Home > 语言编程 > PHP5 ,MYSQL数据库操作函数封装类集合

PHP5 ,MYSQL数据库操作函数封装类集合

相信用过PHP的用户都用过这类的封装类吧,网上也有很多类似的发布,不过各有各长现在我就把我本人的发布出来,供PHP初学者使用,也欢迎专家批评指正,在此谢过了

只能PHP5下使用,PHP4的话可以修改以后再用吧。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!--p
//By CG
class db {
   //public
    public static $conn;
    public static $data;
    public static $fields;
    public static $row;
    public static $row_num;
    public static $insertid;
    public static $version;
    public static $affected_rows;
    public static $query_num = 0;
    public static $debug = false;
   //private
    private static $user;
    private static $pass;
    private static $host;
    private static $db;
 
    $host 服务器地址
    $user 用户名
    $pass 密码
    $db 数据库名称
 
	//构造函数
    function __construct($host,$user,$pass,$db)
    {
		self::$host = $host;
    	self::$pass = $pass;
    	self::$user = $user;
    	self::$db = $db;
        self::Connect();
    }//__construct()
 
    //析构函数
    function __destruct()
    {
     	self::close();
    }//__destruct()
 
    public static function close() {
	if(self::$conn!=NULL)
    	mysql_close(self::$conn);
    }
 
	//connect
    public static function Connect() {
 
    self::$conn =  mysql_connect(self::$host,self::$user,self::$pass) or self::msg("连接数据库失败!可能是mysql数据库用户名或密码不正确");
    self::selectdb(self::$db);
    }
 
    public static function query($sql) {
    $query =  mysql_query($sql,self::$conn) or self::msg("SQL语法错误:".htmlspecialchars($sql));
    if(self::$debug) {
    echo $sql . "n";
    }
    self::query_num();
    return $query;
    }
 
    public static function fetch_array($query) {
    self::$data = mysql_fetch_array($query);
    return self::$data;
    } //这里返回全部的key
 
    public static function fetch_assoc($query) {
    self::$data = mysql_fetch_assoc($query , MYSQL_ASSOC);
    return self::$data;
    }//这里只返回key索引
 
    public static function fetch_num($query) {
    self::$data = mysql_fetch_num($query , MYSQL_NUM);
    return self::$data;
    }//这里只返回数字索引
 
    public static function num_fields($query) {
    self::$fields = mysql_num_fields($query);
    return self::$fields;
    }
 
    public static function fetch_row($query) {
    self::$row = mysql_fetch_row($query);
    return self::$row;
    }
 
    public static function num_rows($query) {
    self::$row_num = mysql_num_rows($query);
    return self::$row_num;
    }
 
    public static function insert_id() {
    self::$insertid = mysql_insert_id();
    return self::$insertid;
    }
 
    public static function affected_rows() {
    self::$affected_rows = mysql_affected_rows(self::$conn);
    return self::$affected_rows;
    }
 
    public static function fetch_one_array($sql){
    $query = self::query($sql);
    self::$data = self::fetch_array($query);
    return self::$data;
    }
 
    public static function free($data)
    {
	if($data) mysql_free_result($data);
    }
 
    private static function query_num(){
    self::$query_num++;
    }
 
    private static function selectdb($db) {
    mysql_select_db($db,self::$conn) or self::msg("未找到指定数据库!");
    }
 
    private static function version() {
    self::$version = mysql_get_server_info();
    return self::$version;
    }
 
    private static function geterror() {
    return mysql_error();
    }
 
    private static function geterrno() {
    return intval(mysql_errno());
    }
 
   private static function error($info) {
	echo $info;
	echo "";
   }
 
    private static function msg($info) {
    echo "<strong>错误信息:</strong>";
    echo "<b>Mysql error:</b>".self::geterror()."";
    echo "<b>Mysql error number:</b>".self::geterrno()."n";
    echo "<b>Time</b>: ".gmdate("Y-n-j g:ia", time() + (8 * 3600))."n";
    echo "<b>Script</b>: ".$_SERVER['PHP_SELF']."n";
    exit;
    }
   }
  -->
Categories: 语言编程 Tags: ,
  1. December 14th, 2008 at 20:57 | #1

    私藏的该顶,记号一下,以后肯定用得着

    已经加上你的链接 :)

  2. July 12th, 2012 at 18:37 | #2

    怎样使用呀???杂的不写上呢?

    • CG
      July 14th, 2012 at 13:03 | #3

      把这些代码保存为一个文件,使用时候导入它,调用时 db->方法名(参数列表)就可以使用了

  1. No trackbacks yet.