-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.php
53 lines (49 loc) · 1.2 KB
/
db.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
<?php
/**
* 数据库连接时封装
*/
class Db
{
//存储类的实例的静态成员变量
private static $_instance;
//数据库连接静态变量
private static $_connectSource;
//连接数据库的配置
private $_dbConfig = array(
'host' => 127.0.0.1,
'user' => 'root',
'password' => '',
'database' => 'demo',
);
private function __construct()
{
# code...
}
/**
*实例化
*/
public static function getInstance() {
//判断是否被实例化
if (!(self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
*数据库连接
*/
public function connect() {
if (!self::$_connectSource) {
//连接数据库
self::$_connectSource = @mysql_connect($this->_dbConfig['host'],
$this->_dbConfig['user'],
$this->_dbConfig['password']);
if (!self::$_connectSource) {
throw new Exception("mysql connect error");
}
mysql_select_db($this->_dbConfig,self::$_connectSource);
mysql_query("set names UTF8",self::$_connectSource);
return self::$_connectSource;
}
}
}