-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.db.php
85 lines (71 loc) · 2.31 KB
/
class.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
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
<?php
class DB {
protected static $connection;
static $db_host;
static $db_user;
static $db_pass;
static $db_name;
public static function init($sever){
$info = parse_ini_file("config.ini",true);
$db = $info[$sever];
self::$db_host = $db['host'];
self::$db_user = $db['user'];
self::$db_pass = $db['pass'];
self::$db_name = $db['name'];
}
public static function connect() {
try {
self::$connection = new PDO('mysql:host='.self::$db_host.';dbname='.self::$db_name, self::$db_user, self::$db_pass);
self::$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
public static function disconnect() {
self::$connection=null;
}
public static function query($query, $value) {
try {
$stmt = self::$connection->prepare($query);
$value == null ? $stmt->execute() : $stmt->execute($value);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $rows;
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
public static function getResult($query, $value, $per_page) {
try {
$stmt = self::$connection->prepare($query);
$value == null ? $stmt->execute() : $stmt->execute($value);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$count = $stmt->rowCount();
return array('total'=>$count, 'per_page'=>$per_page, 'data'=>$rows );
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
public static function execute($query, $value) {
try {
$stmt = self::$connection->prepare($query);
return $value == null ? $stmt->execute() : $stmt->execute($value);
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
public static function getRow($query, $value) {
try {
$stmt = self::$connection->prepare($query);
$value == null ? $stmt->execute() : $stmt->execute($value);
$rows = $stmt->fetch(PDO::FETCH_ASSOC);
return $rows;
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
}