forked from chenlinzhong/php-delayqueue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDqMysql.php
More file actions
111 lines (95 loc) · 3.27 KB
/
DqMysql.php
File metadata and controls
111 lines (95 loc) · 3.27 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
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
<?php
include_once dirname(__FILE__).'/../DqLoader.php';
class DqMysql
{
static $objMysql = null;
static public function getDbInstance()
{
static $time=0;
if(time() - $time >10){
self::$objMysql = null;
}
if(empty($time) || empty(self::$objMysql)) {
$dbms = 'mysql'; //数据库类型
$host = DqConf::$db['host']; //数据库主机名
$port = DqConf::$db['port'];
$dbName = DqConf::$db['database']; //使用的数据库
$user = DqConf::$db['user']; //数据库连接用户名
$pass = DqConf::$db['password']; //对应的密码
$dsn = "$dbms:host=$host;dbname=$dbName;port=$port;";
try {
$dbh = new PDO($dsn, $user, $pass,
array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_TIMEOUT => 3)
); //初始化一个PDO对象
self::$objMysql = $dbh;
$time = time();
return $dbh;
} catch (PDOException $e) {
return null;
}
}
return self::$objMysql;
}
public static function insertData($table, $arrDatas)
{
$fileds = implode(',', array_keys($arrDatas));
if (empty($fileds)) {
echo 'empty data' . "\n";
return;
}
$sql = 'replace INTO ' . $table . ' (' . $fileds . ') VALUES ';
$str = '';
$str .= '("' . implode('","', $arrDatas) . '"),';
$str = trim($str, ',');
$sql .= $str;
return self::getDbInstance()->query($sql);
}
public static function updateData($table, $arrDatas, $condition)
{
$fileds = '';
foreach ($arrDatas as $k => $v) {
$fileds .= $k . "='" . $v . "',";
}
$fileds = trim($fileds, ',');
$sql = 'update ' . $table . ' set ' . $fileds . ' where ' . $condition;
return self::getDbInstance()->query($sql);
}
public static function select($table, $condition = '', $page = 1, $size = 10000)
{
$sql = 'select * from ' . $table;
if (!empty($condition)) {
$sql .= ' where ' . $condition;
}
$start = ($page - 1) * $size;
$sql .= ' order by id desc ';
$sql .= ' limit ' . $start . ',' . $size;
$obj = self::getDbInstance();
if(empty($obj)){
return array();
}
$statement = $obj->prepare($sql);
$statement->execute();
$arr = $statement->fetchAll(PDO::FETCH_ASSOC);
return $arr;
}
public static function selectCount($table, $condition = '')
{
$sql = 'select count(*) as total from ' . $table;
if (!empty($condition)) {
$sql .= ' where ' . $condition;
}
$sql .= ' order by id desc ';
$obj = self::getDbInstance();
if(empty($obj)){
return 0;
}
$statement = $obj->prepare($sql);
$statement->execute();
$arr = $statement->fetchAll(PDO::FETCH_ASSOC);
return isset($arr[0]['total']) ? $arr[0]['total'] : 0;
}
public static function delete($table,$id){
$sql = 'delete from '.$table.' where id='.$id;
return self::getDbInstance()->query($sql);
}
}