1111 *
1212 * A PHP MySQL PDO class similar to the the Python MySQLdb.
1313 */
14- require (dirname (__FILE__ ). "/PDO.Log.class.php " );
14+ require (dirname (__FILE__ ) . "/PDO.Log.class.php " );
1515class DB
1616{
1717 private $ Host ;
@@ -24,152 +24,151 @@ class DB
2424 private $ log ;
2525 private $ parameters ;
2626 public $ querycount = 0 ;
27-
28-
29- public function __construct ($ Host , $ DBName , $ DBUser , $ DBPassword )
30- {
31- $ this ->log = new Log ();
32- $ this ->Host = $ Host ;
33- $ this ->DBName = $ DBName ;
34- $ this ->DBUser = $ DBUser ;
35- $ this ->DBPassword = $ DBPassword ;
36- $ this ->Connect ();
37- $ this ->parameters = array ();
27+
28+
29+ public function __construct ($ Host , $ DBName , $ DBUser , $ DBPassword )
30+ {
31+ $ this ->log = new Log ();
32+ $ this ->Host = $ Host ;
33+ $ this ->DBName = $ DBName ;
34+ $ this ->DBUser = $ DBUser ;
35+ $ this ->DBPassword = $ DBPassword ;
36+ $ this ->Connect ();
37+ $ this ->parameters = array ();
38+ }
39+
40+
41+ private function Connect ()
42+ {
43+ try {
44+ $ this ->pdo = new PDO ('mysql:dbname= ' . $ this ->DBName . ';host= ' . $ this ->Host , $ this ->DBUser , $ this ->DBPassword , array (
45+ PDO ::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8 " ,
46+ PDO ::ATTR_EMULATE_PREPARES => false ,
47+ PDO ::ATTR_ERRMODE => PDO ::ERRMODE_EXCEPTION ,
48+ //PDO::ATTR_PERSISTENT => true,//长连接
49+ PDO ::MYSQL_ATTR_USE_BUFFERED_QUERY => true
50+
51+
52+ ));
53+ $ this ->bConnected = true ;
3854 }
55+ catch (PDOException $ e ) {
56+ echo $ this ->ExceptionLog ($ e ->getMessage ());
57+ die ();
58+ }
59+ }
3960
40-
41- private function Connect ()
42- {
43- try
44- {
45- $ this ->pdo = new PDO ('mysql:dbname= ' .$ this ->DBName .';host= ' .$ this ->Host , $ this ->DBUser , $ this ->DBPassword , array (//PDO::ATTR_PERSISTENT => true,
46- PDO ::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8 "
47- ));
48- $ this ->pdo ->setAttribute (PDO ::MYSQL_ATTR_USE_BUFFERED_QUERY , true );
49- $ this ->pdo ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
50- $ this ->pdo ->setAttribute (PDO ::ATTR_EMULATE_PREPARES , false );
51- $ this ->bConnected = true ;
52- }
53- catch (PDOException $ e )
54- {
55- echo $ this ->ExceptionLog ($ e ->getMessage ());
56- die ();
57- }
61+
62+ public function CloseConnection ()
63+ {
64+ $ this ->pdo = null ;
65+ }
66+
67+
68+ private function Init ($ query , $ parameters = "" )
69+ {
70+ if (!$ this ->bConnected ) {
71+ $ this ->Connect ();
5872 }
59-
60-
61- public function CloseConnection ()
62- {
63- $ this ->pdo = null ;
64- }
65-
66-
67- private function Init ($ query ,$ parameters = "" )
68- {
69- if (!$ this ->bConnected ) { $ this ->Connect (); }
70- try {
71- $ this ->parameters = $ parameters ;
72- $ this ->sQuery = $ this ->pdo ->prepare ($ this ->BuildParams ($ query ,$ this ->parameters ));
73-
74- if (!empty ($ this ->parameters )) {
75- if (array_key_exists (0 , $ parameters ))
76- {
77- $ parametersType = true ;
78- array_unshift ($ this ->parameters , "" );
79- unset($ this ->parameters [0 ]);
80- }else {
81- $ parametersType = false ;
82- }
83- foreach ($ this ->parameters as $ column => $ value )
84- {
85- $ this ->sQuery ->bindParam ($ parametersType ? intval ($ column ) : ": " .$ column , $ this ->parameters [$ column ]);//It would be query after loop end(before 'sQuery->execute()').It is wrong to use $value.
86- }
73+ try {
74+ $ this ->parameters = $ parameters ;
75+ $ this ->sQuery = $ this ->pdo ->prepare ($ this ->BuildParams ($ query , $ this ->parameters ));
76+
77+ if (!empty ($ this ->parameters )) {
78+ if (array_key_exists (0 , $ parameters )) {
79+ $ parametersType = true ;
80+ array_unshift ($ this ->parameters , "" );
81+ unset($ this ->parameters [0 ]);
82+ } else {
83+ $ parametersType = false ;
8784 }
88-
89- $ this ->succes = $ this ->sQuery ->execute ();
90- $ this ->querycount ++;
91- }
92- catch (PDOException $ e )
93- {
94- echo $ this ->ExceptionLog ($ e ->getMessage (), $ this ->BuildParams ($ query ) );
95- die ();
96- }
97-
98- $ this ->parameters = array ();
99- }
100-
101- private function BuildParams ($ query , $ params = null )
102- {
103- if (!empty ($ params )) {
104- $ rawStatement = explode (" " , $ query );
105- foreach ($ rawStatement as $ value ) {
106- if (strtolower ($ value )=='in ' )
107- {
108- return str_replace ("(?) " , "( " .implode (", " ,array_fill (0 ,count ($ params ), "? " )).") " , $ query );
109- }
85+ foreach ($ this ->parameters as $ column => $ value ) {
86+ $ this ->sQuery ->bindParam ($ parametersType ? intval ($ column ) : ": " . $ column , $ this ->parameters [$ column ]); //It would be query after loop end(before 'sQuery->execute()').It is wrong to use $value.
11087 }
11188 }
112- return $ query ;
89+
90+ $ this ->succes = $ this ->sQuery ->execute ();
91+ $ this ->querycount ++;
92+ }
93+ catch (PDOException $ e ) {
94+ echo $ this ->ExceptionLog ($ e ->getMessage (), $ this ->BuildParams ($ query ));
95+ die ();
11396 }
114-
11597
116- public function query ($ query ,$ params = null , $ fetchmode = PDO ::FETCH_ASSOC )
117- {
118- $ query = trim ($ query );
98+ $ this ->parameters = array ();
99+ }
100+
101+ private function BuildParams ($ query , $ params = null )
102+ {
103+ if (!empty ($ params )) {
119104 $ rawStatement = explode (" " , $ query );
120- $ this ->Init ($ query ,$ params );
121- $ statement = strtolower ($ rawStatement [0 ]);
122- if ($ statement === 'select ' || $ statement === 'show ' ) {
123- return $ this ->sQuery ->fetchAll ($ fetchmode );
124- }
125- elseif ( $ statement === 'insert ' || $ statement === 'update ' || $ statement === 'delete ' ) {
126- return $ this ->sQuery ->rowCount ();
127- } else {
128- return NULL ;
105+ foreach ($ rawStatement as $ value ) {
106+ if (strtolower ($ value ) == 'in ' ) {
107+ return str_replace ("(?) " , "( " . implode (", " , array_fill (0 , count ($ params ), "? " )) . ") " , $ query );
108+ }
129109 }
130110 }
131-
111+ return $ query ;
112+ }
132113
133- public function lastInsertId () {
134- return $ this ->pdo ->lastInsertId ();
135- }
136-
137-
138- public function column ($ query ,$ params = null )
139- {
140- $ this ->Init ($ query ,$ params );
141- return $ this ->sQuery ->fetchAll (PDO ::FETCH_COLUMN );
142-
143- }
144-
145-
146- public function row ($ query ,$ params = null ,$ fetchmode = PDO ::FETCH_ASSOC )
147- {
148- $ this ->Init ($ query ,$ params );
149- $ resuleRow = $ this ->sQuery ->fetch ($ fetchmode );
150- $ this ->sQuery ->closeCursor ();
151- return $ resuleRow ;
152- }
153-
154-
155- public function single ($ query ,$ params = null )
156- {
157- $ this ->Init ($ query ,$ params );
158- return $ this ->sQuery ->fetchColumn ();
114+
115+ public function query ($ query , $ params = null , $ fetchmode = PDO ::FETCH_ASSOC )
116+ {
117+ $ query = trim ($ query );
118+ $ rawStatement = explode (" " , $ query );
119+ $ this ->Init ($ query , $ params );
120+ $ statement = strtolower ($ rawStatement [0 ]);
121+ if ($ statement === 'select ' || $ statement === 'show ' ) {
122+ return $ this ->sQuery ->fetchAll ($ fetchmode );
123+ } elseif ($ statement === 'insert ' || $ statement === 'update ' || $ statement === 'delete ' ) {
124+ return $ this ->sQuery ->rowCount ();
125+ } else {
126+ return NULL ;
159127 }
160-
161-
162- private function ExceptionLog ($ message , $ sql = "" )
128+ }
129+
130+
131+ public function lastInsertId ()
132+ {
133+ return $ this ->pdo ->lastInsertId ();
134+ }
135+
136+
137+ public function column ($ query , $ params = null )
163138 {
164- $ exception = 'Unhandled Exception. <br /> ' ;
139+ $ this ->Init ($ query , $ params );
140+ return $ this ->sQuery ->fetchAll (PDO ::FETCH_COLUMN );
141+
142+ }
143+
144+
145+ public function row ($ query , $ params = null , $ fetchmode = PDO ::FETCH_ASSOC )
146+ {
147+ $ this ->Init ($ query , $ params );
148+ $ resuleRow = $ this ->sQuery ->fetch ($ fetchmode );
149+ $ this ->sQuery ->closeCursor ();
150+ return $ resuleRow ;
151+ }
152+
153+
154+ public function single ($ query , $ params = null )
155+ {
156+ $ this ->Init ($ query , $ params );
157+ return $ this ->sQuery ->fetchColumn ();
158+ }
159+
160+
161+ private function ExceptionLog ($ message , $ sql = "" )
162+ {
163+ $ exception = 'Unhandled Exception. <br /> ' ;
165164 $ exception .= $ message ;
166165 $ exception .= "<br /> You can find the error back in the log. " ;
167-
168- if (!empty ($ sql )) {
169- $ message .= "\r\nRaw SQL : " . $ sql ;
166+
167+ if (!empty ($ sql )) {
168+ $ message .= "\r\nRaw SQL : " . $ sql ;
170169 }
171- $ this ->log ->write ($ message );
172-
170+ $ this ->log ->write ($ message );
171+
173172 return $ exception ;
174- }
173+ }
175174}
0 commit comments