PHP连接多个mysql数据库的类

mysql.php类

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
/*
* 定义一个连接类,可以访问建立多个数据库连接对象
*
*/
class mysql{
var $dbhost; //数据库地址
var $dbuser; //数据库用户名
var $dbpasswd; //数据库密码
var $dbpconnect = 0; //数据库长连接
var $dbname; //数据库名称
var $dbchart; //数据库链接编码
var $dblink; //数据库连接对象
var $sql; //sql语句
var $res; //sql语句执行结果
var $errno; //错误信息

function connect($dbConfig) {
$this->dbhost = $dbConfig['dbhost'];
$this->dbuser = $dbConfig['dbuser'];
$this->dbpasswd = $dbConfig['dbpasswd'];
$this->dbpconnect = $dbConfig['dbpconnect'];
$this->dbname = $dbConfig['dbname'];
$this->dbchart = $dbConfig['dbchart'];
if($this->dbpconnect) {
$this->dblink = mysql_pconnect($this->dbhost,$this->dbuser,$this->dbpasswd,1)
or die('can not connect to mysql database!');
} else {
$this->dblink = mysql_connect($this->dbhost,$this->dbuser,$this->dbpasswd,1)
or die('can not connect to mysql database!');
}
mysql_query('set names '.$this->dbchart, $this->dblink);
mysql_select_db($this->dbname, $this->dblink);
unset($dbConfig);
}

/**
* 数据库执行语句
*
* @return blooean
*
*/
function query($sql, $die_msg = 1) {
$this->sql = $sql;
//可以用自定义错误信息的方法,就要压制本身的错误信息
$result = @mysql_query($sql, $this->dblink);
if($result == true) {
return $result;
}else{
//有错误发生
$this->errno = mysql_error($this->dblink);
if($this->errno >0) {
if($die_msg == 1) {
//强制报错并且die
$this->msg();
}else{
return $this->errno;//无强制报错,则返回错误代码
}
}
}
}

/**
* 获得查询语句单条结果
*
* @return array
*
*/
function getOne($sql) {
$this->sql = $sql;
$this->res = $this->query($sql);
return mysql_fetch_assoc($this->res);
}

/**
* 获得查询语句多条结果
*
* @return array
*
*/
function getAll($sql) {
$this->sql = $sql;
$this->res = $this->query($sql);
$arr = array();
while($row = mysql_fetch_assoc($this->res)) {
$arr[] = $row;
}
return $arr;
}

/**
* 取得结果数据
*
* @param resource $query
*
* @return string
*
*/
function result($query, $row) {
$query = @mysql_result($query, $row);
return $query;
}

/**
* 获得刚插入数据的id
*
* @return int id
*
*/
function getInsertID() {
return ($id = mysql_insert_id($this->dblink)) >= 0 ? $id :
$this->result($this->query('SELECT last_insert_id()'), 0);
}

/**
* 关闭数据库连接,当您使用持续连接时该功能失效
*
* @return blooean
*
*/
function close() {
return mysql_close($this->dblink);
}

/**
* 显示自定义错误
*
*/
function msg() {
if($this->errno) {
//可以根据错误ID,配置好一些友好的错误信息提示语句
$msgArr = array();
$msgArr['1062'] = "唯一性索引有重复值插入";
/*...更多错误代码根据实际业务再添加...*/
if($msgArr[$this->errno]) {
$errMsg = $msgArr[$this->errno];//已定义的错误
}else{
$errMsg = mysql_error();//未定义的错误,由默认的错误信息决定
}
echo "<div style='color:red;'>\n";
echo "<h4>数据库操作错误</h4>\n";
echo "<h5>错误代码:".$this->errno."</h5>\n";
echo "<h5>错误信息:".$errMsg."</h5>\n";
echo "</div>";
unset($msgArr);
die;
}
}

function __destruct() {
$this->dbhost;
$this->dbuser;
$this->dbpasswd;
$this->dbpconnect;
$this->dbname;
$this->dbchart;
$this->dblink;
$this->sql;
$this->res;
$this->errno;
}
}

测试连接

1
2
3
4
5
6
7
8
9
10
11
12
13
$dbConfig1 = array( 'dbhost'     => 'localhost',
'dbuser' => 'root',
'dbpasswd' => '',
'dbname' => 'server',
'dbpconnect' => 0,
'dbchart' => 'utf8');
$db1 = new mysql();
$db1->connect($dbConfig1);

$arr = $db1->getOne("select * from user");
print_r($arr);

$db1->close();