55use \PDO ;
66use \PDOException ;
77
8- class DBPDO {
8+ class DBPDO
9+ {
910
11+ private static $ instance = null ;
1012 public $ pdo ;
1113 private $ error ;
1214 private $ dbname ;
1315 private $ dbhost ;
1416 private $ dbuser ;
1517 private $ dbpass ;
18+ private $ orderwise ;
1619
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+ }
1727
18- function __construct ($ dbhost = '' , $ dbname = '' , $ dbuser = '' , $ dbpass = '' ) {
28+ function __construct ($ dbhost = '' , $ dbname = '' , $ dbuser = '' , $ dbpass = '' , $ orderwise = false )
29+ {
1930 $ this ->dbhost = $ dbhost ;
2031 $ this ->dbname = $ dbname ;
2132 $ this ->dbuser = $ dbuser ;
2233 $ this ->dbpass = $ dbpass ;
34+ $ this ->orderwise = $ orderwise ;
2335 $ this ->connect ();
2436 }
2537
38+ // Disallow cloning and unserializing
39+ private function __clone () {}
40+ private function __wakeup () {}
41+
2642
27- function prep_query ($ query ){
43+ function prep_query ($ query )
44+ {
2845 return $ this ->pdo ->prepare ($ query );
2946 }
3047
3148
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+ }
3657 $ user = $ this ->dbuser ;
3758 $ password = $ this ->dbpass ;
3859
3960 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+ }
4166 return true ;
4267 } catch (PDOException $ e ) {
4368 $ this ->error = $ e ->getMessage ();
44- die ($ this ->error );
69+ // die($this->error);
4570 return false ;
4671 }
47- }else {
48- $ this ->pdo ->setAttribute ( PDO ::ATTR_ERRMODE , PDO ::ERRMODE_WARNING );
72+ } else {
73+ $ this ->pdo ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_WARNING );
4974 return true ;
5075 }
5176 }
5277
5378
54- function table_exists ($ table_name ){
79+ function table_exists ($ table_name )
80+ {
5581 $ stmt = $ this ->prep_query ('SHOW TABLES LIKE ? ' );
5682 $ stmt ->execute (array ($ table_name ));
5783 return $ stmt ->rowCount () > 0 ;
5884 }
5985
6086
61- function execute ($ query , $ values = null ){
62- if ($ values == null ){
87+ function execute ($ query , $ values = null , $ debug = false )
88+ {
89+ if ($ values == null ) {
6390 $ values = array ();
64- }else if (!is_array ($ values )){
91+ } else if (!is_array ($ values )) {
6592 $ values = array ($ values );
6693 }
6794 $ 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+ }
69107 return $ stmt ;
70108 }
71109
72- function fetch ($ query , $ values = null ){
73- if ($ values == null ){
110+ function fetch ($ query , $ values = null )
111+ {
112+ if ($ values == null ) {
74113 $ values = array ();
75- }else if (!is_array ($ values )){
114+ } else if (!is_array ($ values )) {
76115 $ values = array ($ values );
77116 }
78117 $ stmt = $ this ->execute ($ query , $ values );
79118 return $ stmt ->fetch (PDO ::FETCH_ASSOC );
80119 }
81120
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 ) {
84124 $ values = array ();
85- }else if (!is_array ($ values )){
125+ } else if (!is_array ($ values )) {
86126 $ values = array ($ values );
87127 }
88128 $ stmt = $ this ->execute ($ query , $ values );
@@ -91,7 +131,7 @@ function fetchAll($query, $values = null, $key = null){
91131 // Allows the user to retrieve results using a
92132 // column from the results as a key for the array
93133 if (!empty ($ results )){
94- if ($ key != null && $ results [ 0 ][ $ key ] ) {
134+ if ($ key != null ) {
95135 $ keyed_results = array ();
96136 foreach ($ results as $ result ) {
97137 $ keyed_results [$ result [$ key ]] = $ result ;
@@ -102,8 +142,8 @@ function fetchAll($query, $values = null, $key = null){
102142 return $ results ;
103143 }
104144
105- function lastInsertId (){
145+ function lastInsertId ()
146+ {
106147 return $ this ->pdo ->lastInsertId ();
107148 }
108-
109149}
0 commit comments