11<?php 
22
33/** 
4- *  
5- */ 
6- class  DB  {
4+  *  
5+  */ 
6+ class  DB 
7+ {
78	private  static  $ _instance  = null ;
89	private  $ _pdo ,
9- 			$ _query ,
10- 			$ _error  = false ,
11- 			$ _results ,
12- 			$ _count  = 0 ;
13- 	
14- 	private  function  __construct (){
15- 		try {
10+ 		$ _query ,
11+ 		$ _error  = false ,
12+ 		$ _results ,
13+ 		$ _count  = 0 ;
14+ 
15+ 	private  function  __construct ()
16+ 	{
17+ 		try  {
1618			/* The syntex here basically looks like this  
1719			    *  $this->_pdo = new PDO('mysql:host=localhost;dbname=quran;charset=utf8','root',''); 
1820			*/ 
19- 			$ this  ->_pdo  = new  PDO ('mysql:host= ' . Config::get ('mysql/host ' ). ';dbname= ' . Config::get ('mysql/database ' ). ';charset= ' . Config::get ('mysql/charset ' ), Config::get ('mysql/username ' ), Config::get ('mysql/password ' ));
21+ 			$ this  ->_pdo  = new  PDO ('mysql:host= '  .  Config::get ('mysql/host ' ) .  ';dbname= '  .  Config::get ('mysql/database ' ) .  ';charset= '  .  Config::get ('mysql/charset ' ), Config::get ('mysql/username ' ), Config::get ('mysql/password ' ));
2022
2123			// Uncomment to be confirmed about Database Connection 
2224			// echo "database connected!"; 
2325
24- 		} catch (PDOException   $ e ){
26+ 		} catch   (PDOException   $ e )  {
2527			die ($ e ->getMessage ());
2628		}
2729	}
2830
2931
3032
31- 	public  static  function  operation (){
33+ 	public  static  function  operation ()
34+ 	{
3235		// To Prevent multiple connection on same page 
33- 		if (!isset (self ::$ _instance )){
36+ 		if   (!isset (self ::$ _instance ))  {
3437			self ::$ _instance  = new  DB ();
3538		}
3639		return  self ::$ _instance ;
@@ -42,42 +45,42 @@ public static function operation(){
4245
4346
4447
45-     private  function  getDataType ($ variable )
46-     {
47-         $ variableType  = gettype ($ variable );
48-         switch  ($ variableType )
49-         {
50-             case  'integer ' :
51-                 return  PDO ::PARAM_INT ;
52-                 break ;
53-             case  'string ' :
54-                 return  PDO ::PARAM_STR ;
55-                 break ;
56-             case  'double ' :
57-                 return  PDO ::PARAM_STR ;
58-                 break ;
59-             case  'boolean ' :
60-                 return  PDO ::PARAM_BOOL ;
61-                 break ;
62-             case  'NULL ' :
63-                 return  PDO ::PARAM_NULL ;
64-                 break ;
65-         }
66-     }
48+ 	private  function  getDataType ($ variable )
49+ 	{
50+ 		$ variableType  = gettype ($ variable );
51+ 		switch  ($ variableType ) {
52+ 			case  'integer ' :
53+ 				return  PDO ::PARAM_INT ;
54+ 				break ;
55+ 			case  'string ' :
56+ 				return  PDO ::PARAM_STR ;
57+ 				break ;
58+ 			case  'double ' :
59+ 				return  PDO ::PARAM_STR ;
60+ 				break ;
61+ 			case  'boolean ' :
62+ 				return  PDO ::PARAM_BOOL ;
63+ 				break ;
64+ 			case  'NULL ' :
65+ 				return  PDO ::PARAM_NULL ;
66+ 				break ;
67+ 		}
68+ 	}
6769
6870
6971
7072
7173
7274
7375
74- 	public  function  query ($ sql , $ params  = array ()) {
76+ 	public  function  query ($ sql , $ params  = array ())
77+ 	{
7578		$ this  ->_error  = false ;
7679
77- 		if ($ this  ->_query  = $ this  ->_pdo ->prepare ($ sql )) {
80+ 		if   ($ this  ->_query  = $ this  ->_pdo ->prepare ($ sql )) {
7881			//to determine possition, check if it should be 0 
7982			$ x  = 1 ;
80- 			if (count ($ params )) {
83+ 			if   (count ($ params )) {
8184				foreach  ($ params  as  $ param ) {
8285					/* This code is like this  
8386					   $this->_query->bindValue(possition, $parameter); 
@@ -91,10 +94,10 @@ public function query($sql, $params = array()) {
9194				}
9295			}
9396
94- 			if ($ this  ->_query ->execute ()) {
97+ 			if   ($ this  ->_query ->execute ()) {
9598				$ this  ->_results  = $ this  ->_query ->fetchAll (PDO ::FETCH_OBJ );
9699				$ this  ->_count  = $ this  ->_query ->rowCount ();
97- 			}   else  {
100+ 			} else  {
98101				$ this  ->_error  = true ;
99102			}
100103		}
@@ -108,38 +111,41 @@ public function query($sql, $params = array()) {
108111
109112
110113
111- 	public  function  action ($ action , $ table , $ where  = array ()) {
112- 		if (count ($ where ) === 3 ) {
114+ 	public  function  action ($ action , $ table , $ where  = array ())
115+ 	{
116+ 		if  (count ($ where ) === 3 ) {
113117			$ operators  = array ('= ' , '> ' , '< ' , '>= ' , '<= ' );
114118
115119			$ field      = $ where [0 ];
116120			$ operator   = $ where [1 ];
117121			$ value      = $ where [2 ];
118122
119- 			if (in_array ($ operator , $ operators )) {
123+ 			if   (in_array ($ operator , $ operators )) {
120124				$ sql  = "{$ action } FROM  {$ table } WHERE  {$ field }  {$ operator } ? " ;
121125
122126				if  (!$ this  ->query ($ sql , array ($ value ))->error ()) {
123127					return  $ this  ;
124128				}
125- 
126129			}
127130		}
128131		return  false ;
129132	}
130133
131134
132- 	public  function  get ($ table , $ where ) {
135+ 	public  function  get ($ table , $ where )
136+ 	{
133137		return  $ this  ->action ('SELECT * ' , $ table , $ where );
134138	}
135139
136140
137- 	public  function  delete ($ table , $ where ) {
141+ 	public  function  delete ($ table , $ where )
142+ 	{
138143		return  $ this  ->action ('DELETE ' , $ table , $ where );
139144	}
140145
141146
142- 	public  function  insert ($ table , $ fields  = array ()) {
147+ 	public  function  insert ($ table , $ fields  = array ())
148+ 	{
143149		if  (count ($ fields )) {
144150			$ keys  = array_keys ($ fields );
145151			$ values  = '' ;
@@ -155,17 +161,17 @@ public function insert($table, $fields = array()) {
155161			}
156162
157163
158- 			$ sql  = "INSERT INTO  {$ table } (` " . implode ('`, ` ' , $ keys ). "`) VALUES( {$ values }) " ;
164+ 			$ sql  = "INSERT INTO  {$ table } (` "  .  implode ('`, ` ' , $ keys ) .  "`) VALUES( {$ values }) " ;
159165
160166			if  (!$ this  ->query ($ sql , $ fields )->error ()) {
161167				return  true ;
162168			}
163- 
164169		}
165170		return  false ;
166171	}
167172
168- 	public  function  update ($ primary_key , $ table , $ fields ) {
173+ 	public  function  update ($ primary_key , $ table , $ fields )
174+ 	{
169175		$ set  = '' ;
170176		$ x  = 1 ;
171177
@@ -186,23 +192,26 @@ public function update($primary_key, $table, $fields) {
186192	}
187193
188194
189- 	public  function  results () {
195+ 	public  function  results ()
196+ 	{
190197		return  $ this  ->_results ;
191198	}
192199
193200
194- 	public  function  firstResult () {
201+ 	public  function  firstResult ()
202+ 	{
195203		return  $ this  ->results ()[0 ];
196204	}
197205
198206
199- 	public  function  error () {
207+ 	public  function  error ()
208+ 	{
200209		return  $ this  ->_error ;
201210	}
202211
203212
204- 	public  function  count () {
213+ 	public  function  count ()
214+ 	{
205215		return  $ this  ->_count ;
206216	}
207- 
208- }
217+ }
0 commit comments