5
5
use \PDO ;
6
6
use \PDOException ;
7
7
8
- class DBPDO {
8
+ class DBPDO
9
+ {
9
10
11
+ private static $ instance = null ;
10
12
public $ pdo ;
11
13
private $ error ;
12
14
private $ dbname ;
13
15
private $ dbhost ;
14
16
private $ dbuser ;
15
17
private $ dbpass ;
18
+ private $ orderwise ;
16
19
20
+ public static function getInstance ($ dbhost , $ dbname , $ dbuser , $ dbpass , $ orderwise = false )
21
+ {
22
+ if (self ::$ instance === null ) {
23
+ self ::$ instance = new self ($ dbhost , $ dbname , $ dbuser , $ dbpass , $ orderwise );
24
+ }
25
+ return self ::$ instance ;
26
+ }
17
27
18
- function __construct ($ dbhost = '' , $ dbname = '' , $ dbuser = '' , $ dbpass = '' ) {
28
+ function __construct ($ dbhost = '' , $ dbname = '' , $ dbuser = '' , $ dbpass = '' , $ orderwise = false )
29
+ {
19
30
$ this ->dbhost = $ dbhost ;
20
31
$ this ->dbname = $ dbname ;
21
32
$ this ->dbuser = $ dbuser ;
22
33
$ this ->dbpass = $ dbpass ;
34
+ $ this ->orderwise = $ orderwise ;
23
35
$ this ->connect ();
24
36
}
25
37
38
+ // Disallow cloning and unserializing
39
+ private function __clone () {}
40
+ private function __wakeup () {}
41
+
26
42
27
- function prep_query ($ query ){
43
+ function prep_query ($ query )
44
+ {
28
45
return $ this ->pdo ->prepare ($ query );
29
46
}
30
47
31
48
32
- function connect (){
33
- if (!$ this ->pdo ){
34
-
35
- $ dsn = 'mysql:dbname= ' . $ this ->dbname . ';host= ' . $ this ->dbhost . ';charset=utf8 ' ;
49
+ function connect ()
50
+ {
51
+ if (!$ this ->pdo ) {
52
+ if ($ this ->orderwise ){
53
+ $ dsn = 'sqlsrv:Server= ' . $ this ->dbhost . ';Database= ' . $ this ->dbname . ';Encrypt=no ' ;
54
+ }else {
55
+ $ dsn = 'mysql:dbname= ' . $ this ->dbname . ';host= ' . $ this ->dbhost . ';charset=utf8mb4 ' ;
56
+ }
36
57
$ user = $ this ->dbuser ;
37
58
$ password = $ this ->dbpass ;
38
59
39
60
try {
40
- $ this ->pdo = new PDO ($ dsn , $ user , $ password , array (PDO ::ATTR_PERSISTENT => true ));
61
+ if ($ this ->orderwise ){
62
+ $ this ->pdo = new PDO ($ dsn , $ user , $ password );
63
+ }else {
64
+ $ this ->pdo = new PDO ($ dsn , $ user , $ password , array (PDO ::ATTR_PERSISTENT => true ));
65
+ }
41
66
return true ;
42
67
} catch (PDOException $ e ) {
43
68
$ this ->error = $ e ->getMessage ();
44
- die ($ this ->error );
69
+ // die($this->error);
45
70
return false ;
46
71
}
47
- }else {
48
- $ this ->pdo ->setAttribute ( PDO ::ATTR_ERRMODE , PDO ::ERRMODE_WARNING );
72
+ } else {
73
+ $ this ->pdo ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_WARNING );
49
74
return true ;
50
75
}
51
76
}
52
77
53
78
54
- function table_exists ($ table_name ){
79
+ function table_exists ($ table_name )
80
+ {
55
81
$ stmt = $ this ->prep_query ('SHOW TABLES LIKE ? ' );
56
82
$ stmt ->execute (array ($ table_name ));
57
83
return $ stmt ->rowCount () > 0 ;
58
84
}
59
85
60
86
61
- function execute ($ query , $ values = null ){
62
- if ($ values == null ){
87
+ function execute ($ query , $ values = null , $ debug = false )
88
+ {
89
+ if ($ values == null ) {
63
90
$ values = array ();
64
- }else if (!is_array ($ values )){
91
+ } else if (!is_array ($ values )) {
65
92
$ values = array ($ values );
66
93
}
67
94
$ stmt = $ this ->prep_query ($ query );
68
- $ stmt ->execute ($ values );
95
+ if ($ debug ){
96
+ echo $ query ;
97
+ print_r ($ values );
98
+ die ();
99
+ }
100
+ try {
101
+ $ stmt ->execute ($ values );
102
+ } catch (PDOException $ e ) {
103
+ $ this ->error = $ e ->getMessage ();
104
+ die ($ query . "<br /> \n" . $ this ->error );
105
+ return false ;
106
+ }
69
107
return $ stmt ;
70
108
}
71
109
72
- function fetch ($ query , $ values = null ){
73
- if ($ values == null ){
110
+ function fetch ($ query , $ values = null )
111
+ {
112
+ if ($ values == null ) {
74
113
$ values = array ();
75
- }else if (!is_array ($ values )){
114
+ } else if (!is_array ($ values )) {
76
115
$ values = array ($ values );
77
116
}
78
117
$ stmt = $ this ->execute ($ query , $ values );
79
118
return $ stmt ->fetch (PDO ::FETCH_ASSOC );
80
119
}
81
120
82
- function fetchAll ($ query , $ values = null , $ key = null ){
83
- if ($ values == null ){
121
+ function fetchAll ($ query , $ values = null , $ key = null )
122
+ {
123
+ if ($ values == null ) {
84
124
$ values = array ();
85
- }else if (!is_array ($ values )){
125
+ } else if (!is_array ($ values )) {
86
126
$ values = array ($ values );
87
127
}
88
128
$ stmt = $ this ->execute ($ query , $ values );
@@ -91,7 +131,7 @@ function fetchAll($query, $values = null, $key = null){
91
131
// Allows the user to retrieve results using a
92
132
// column from the results as a key for the array
93
133
if (!empty ($ results )){
94
- if ($ key != null && $ results [ 0 ][ $ key ] ) {
134
+ if ($ key != null ) {
95
135
$ keyed_results = array ();
96
136
foreach ($ results as $ result ) {
97
137
$ keyed_results [$ result [$ key ]] = $ result ;
@@ -102,8 +142,8 @@ function fetchAll($query, $values = null, $key = null){
102
142
return $ results ;
103
143
}
104
144
105
- function lastInsertId (){
145
+ function lastInsertId ()
146
+ {
106
147
return $ this ->pdo ->lastInsertId ();
107
148
}
108
-
109
149
}
0 commit comments