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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
<?php
/**
* gMysql MySQL数据库的驱动支持
*/
class
gMysql {
/**
* 数据库链接句柄
*/
public
$conn
;
// 当前使用的连接
/**
* 执行的SQL语句记录
*/
public
$arrSql
;
// 当前连接ID
public
$m_link_id
= null;
// 主库连接
public
$s_link_id
= null;
// 从库连接
// 是否多库
public
$multi_server
= false;
// 数据库连接参数配置
public
$dbConfig
=
array
();
public
$dbCurrent
=
array
();
/**
* 构造函数
*
* @param dbConfig 数据库配置
*/
public
function
__construct(
$dbConfig
)
{
$this
->dbConfig =
$dbConfig
;
$this
->multi_server =
empty
(
$this
->dbConfig[
'slave'
] ) ? false : true;
}
/**
* 连接数据库方法
* @param type $dbConfig
*/
public
function
connect (
$db
) {
$this
->dbCurrent =
$db
;
$linkfunction
= ( TRUE ==
$db
[
'persistent'
] ) ?
'mysql_pconnect'
:
'mysql_connect'
;
$this
->conn =
$linkfunction
(
$db
[
'host'
],
$db
[
'username'
],
$db
[
'password'
]);
if
(!
$this
->conn) {
return
false;
}
$re
= mysql_select_db(
$db
[
'dbname'
],
$this
->conn);
if
(!
$re
) {
return
false;
}
mysql_query (
"SET NAMES '"
.
$db
['charset
'] . "'
",
$this
->conn );
}
/**
* 初始化数据库连接
* @param type $master
*/
public
function
initConnect (
$master
= true) {
if
(
$master
|| !
$this
->multi_server) {
if
(
$this
->m_link_id){
$this
->conn =
$this
->m_link_id;
$this
->ping(
$master
);
}
else
{
$this
->connect (
$this
->dbConfig [
'master'
] );
$this
->m_link_id =
$this
->conn;
}
}
else
{
if
(
$this
->s_link_id){
$this
->conn =
$this
->s_link_id;
$this
->ping(
$master
);
}
else
{
$rand
= rand(0,
count
(
$this
->dbConfig [
'slave'
]) - 1);
$this
->connect (
$this
->dbConfig [
'slave'
][
$rand
] );
$this
->s_link_id =
$this
->conn;
}
}
}
/**
* 按SQL语句获取记录结果,返回数组
*
* @param sql 执行的SQL语句
*/
public
function
getArray(
$sql
)
{
if
( !
$result
=
$this
->query(
$sql
) )
return
FALSE;
if
( ! mysql_num_rows(
$result
) )
return
FALSE;
$rows
=
array
();
while
(
$rows
[] = mysql_fetch_array(
$result
,MYSQL_ASSOC)){}
mysql_free_result(
$result
);
array_pop
(
$rows
);
return
$rows
;
}
/**
* 返回当前插入记录的主键ID
*/
public
function
newinsertid()
{
return
mysql_insert_id(
$this
->conn);
}
/**
* 格式化带limit的SQL语句
*/
public
function
setlimit(
$sql
,
$limit
)
{
return
$sql
.
" LIMIT {$limit}"
;
}
/**
* 执行一个SQL语句
*
* @param sql 需要执行的SQL语句
*/
public
function
exec
(
$sql
)
{
$this
->arrSql[] =
$sql
;
$this
->initConnect ( true );
return
mysql_query(
$sql
,
$this
->conn);
}
/**
* 执行一个SQL语句,主要用于查询
* @param type $sql
* @param type $master default:false 为true:强制读主库;为false:在有从库的的情况下优先读从库,否则读主库
*/
public
function
query (
$sql
,
$master
= false) {
$this
->arrSql[] =
$sql
;
$this
->initConnect (
$master
);
return
mysql_query(
$sql
,
$this
->conn);
}
/**
* 返回影响行数
*/
public
function
affected_rows()
{
return
mysql_affected_rows(
$this
->conn);
}
/**
* 获取数据表结构
*
* @param tbl_name 表名称
*/
public
function
getTable(
$tbl_name
)
{
return
$this
->getArray(
"DESCRIBE {$tbl_name}"
);
}
//防止mysql gone away
public
function
ping(
$master
) {
if
(!@mysql_ping(
$this
->conn)){
@mysql_close(
$this
->conn);
//注意:一定要先执行数据库关闭,这是关键
if
(
$master
|| !
$this
->multi_server) {
unset(
$this
->m_link_id);
}
else
{
unset(
$this
->s_link_id);
}
$this
->initConnect(
$master
);
}
}
/**
* 对特殊字符进行过滤
*
* @param value 值
*/
public
function
__val_escape(
$value
) {
if
(
is_null
(
$value
))
return
'NULL'
;
if
(
is_bool
(
$value
))
return
$value
? 1 : 0;
if
(
is_int
(
$value
))
return
(int)
$value
;
if
(
is_float
(
$value
))
return
(float)
$value
;
if
(@get_magic_quotes_gpc())
$value
=
stripslashes
(
$value
);
$this
->conn ||
$this
->initConnect();
return
'\''
.mysql_real_escape_string(
$value
,
$this
->conn).
'\''
;
}
public
function
escape(
$value
) {
if
(
is_null
(
$value
))
return
'NULL'
;
if
(
is_bool
(
$value
))
return
$value
? 1 : 0;
if
(
is_int
(
$value
))
return
(int)
$value
;
if
(
is_float
(
$value
))
return
(float)
$value
;
if
(@get_magic_quotes_gpc())
$value
=
stripslashes
(
$value
);
$this
->conn ||
$this
->initConnect();
return
mysql_real_escape_string(
$value
,
$this
->conn);
}
/**
* 析构函数
*/
public
function
__destruct()
{
if
( TRUE != @
$this
->dbCurrent[
'persistent'
] )@mysql_close(
$this
->conn);
}
}
|
重点在 initConnect($master)方法里,这里决定加载的配置文件中是连接到主库还是从库
本文转自 陈小龙哈 51CTO博客,原文链接:http://blog.51cto.com/chenxiaolong/1828506