08
2

php单例模式的mysql数据访问类

最近在弄PHPRPC,才发现原来的mysql数据类不能用,在Q群上,群主建议我用单例模式实现db类,网上找来这个试试.

使用方法

$db=DbMySql::get_instance(‘localhost’,'linji’,'root’,'12345678′,’utf8′);

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
<?php
class DbMySql
{
     private $db;
     private $dbName;
     static $instance;
 
     private function __construct($dbHost, $dbName, $dbUser, $dbPwd, $charset = 'utf8')
    {
         $this -> db = mysql_connect($dbHost, $dbUser, $dbPwd) or die('Could not connect: ' . mysql_error());
         $this -> select_db($dbName);
         $this -> query("SET NAMES $charset");
 
         }
 
     private function __clone(){
    }
 
     public static function get_instance($dbHost, $dbName, $dbUser, $dbPwd, $charset)
    {
         if(!(self :: $instance instanceof self))
            {
             self :: $instance = new self($dbHost, $dbName, $dbUser, $dbPwd, $charset);
             }
         return self :: $instance;
         }
 
    /**
     * 查询表
     * 返回一个一维数组
     */
     public function get_one($sql)
    {
         $query = $this -> query($sql);
         $rs = $this -> fetch_array($query);
         $this -> free_result($query);
         return $rs ;
         }
 
    /**
     * 查询表
     * 返回一个二维数组
     */
     public function select($sql, $keyfield = '')
    {
         $array = array();
         $result = $this -> query($sql);
         while($r = $this -> fetch_array($result))
        {
             if($keyfield)
            {
                 $key = $r[$keyfield];
                 $array[$key] = $r;
                 }
            else
                {
                 $array[] = $r;
                 }
             }
         $this -> free_result($result);
         return $array;
         }
 
    /**
     * 修改数据
     * 
     * @param tableName $ 操作的表
     * @param array $ 信息数组
     * @param where $ 条件
     */
     function update($tableName, $array, $where = '')
    {
         if($where)
        {
             $sql = '';
             foreach($array as $k => $v)
            {
                 $sql .= ", `$k`='$v'";
                 }
             $sql = substr($sql, 1);
             $sql = "UPDATE $tablename SET $sql WHERE $where";
             }
        else
            {
             $sql = "REPLACE INTO $tablename(`" . implode('`,`', array_keys($array)) . "`) VALUES('" . implode("','", $array) . "')";
             }
         return $this -> query($sql);
         }
 
    /**
     * 添加数据
     * 
     * @param tableName $ 操作的表
     * @param array $ 信息数组
     * return int 信息id
     */
     function insert($tableName, $array)
    {
         $this -> query("INSERT INTO $tableName(`" . implode('`,`', array_keys($array)) . "`) VALUES('" . implode("','", $array) . "')");
         return $this -> insert_id();
         }
 
     public function query($sql)
    {
         return mysql_query($sql, $this -> db);
         }
 
     public function fetch_array($query, $result_type = MYSQL_ASSOC)
    {
         return mysql_fetch_array($query, $result_type);
         }
 
     public function select_db($dbName)
    {
         mysql_select_db($dbName , $this -> db) or die('Could not select database');
         $this -> dbName = $dbName;
         }
 
     public function close()
    {
         return mysql_close($this -> db);
         }
 
     public function free_result(& $query)
    {
         return mysql_free_result($query);
         }
 
     public function insert_id()
    {
         return mysql_insert_id($this -> db);
         }
 
     public function fetch_row($query)
    {
         return mysql_fetch_row($query);
         }
 
     public function affected_rows()
    {
         return mysql_affected_rows($this -> db);
         }
 
     public function num_rows($query)
    {
         return mysql_num_rows($query);
         }
    }
?>

相关日志

当前没有评论!

第一个在本文留言。

发表评论

名字(必须)
邮箱(必须),(永不被公布)
网址(建议)

字体为 粗体 是必填项目,邮箱地址 永远不会 公布。

允许部分 HTML 代码:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
URLs(网站链接)必须完整有效 (比如: http://www.vpnall.com),所有标签都必须完整的关闭。

超出部分系统将会自动分段及换行。

请保证评论内容是与日志或 Blog 内容相关的,灌水、攻击性或不恰当的评论 可能 会被编辑或删除。