-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
48 lines (41 loc) · 1.06 KB
/
Copy pathdb.php
File metadata and controls
48 lines (41 loc) · 1.06 KB
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
<?php
/**
数据库单例类
*/
class Db {
/** 存储数据库单例 */
static private $_instance;
static private $_connectSource;
private $_dbConfig = array(
'host' => '127.0.0.1',
'user' => 'root',
'password' => 'root',
'database' => 'test'
);
private function __construct() {
}
static public function getInstance() {
if(!(self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
}
public function connect() {
self::$_connectSource = mysql_connect($this->_dbConfig['host'], $this->_dbConfig['user'], $this->_dbConfig['password']);
if(!self::$_connectSource) {
throw new Exception('mysql connect error'.mysql_error());
// die('mysql connect error', mysql_error());
}
mysql_select_db($this->_dbConfig['database'], self::$_connectSource);
mysql_query("SET NAMES UTF8", self::$_connectSource);
return self::$_connectSource;
}
}
/**
Example:
$connect = Db::getInstance()->connect();
$sql = "SELECT * from test";
$result = mysql_query($sql);
echo mysql_num_rows($result);
var_dump($result);
*/