11<?php
22 class SmysqlException extends Exception {
33 function __construct ($ e , $ code = 0 , Exception $ previous = NULL ) {
4- $ this ->message = "<strong>Simon's MySQL error</strong> " . $ e . " in <strong> " . parent ::getFile () . "</strong> on line <strong> " . parent ::getLine () . "</strong> " ;
4+ $ this ->message = "<strong>Simon's MySQL error</strong> " . $ e . " in <strong> " . end ( parent ::getTrace ())[ ' file ' ] . "</strong> on line <strong> " . end ( parent ::getTrace ())[ ' line ' ] . "</strong> " ;
55 }
66 function __toString () {
77 trigger_error ($ this ->message , E_USER_ERROR );
@@ -27,7 +27,7 @@ class Smysql {
2727 const FETCH_OBJECT = 256 ;
2828 const FETCH_ARRAY = 512 ;
2929 const FETCH_ALL = 1024 ;
30- const ALLWAYS_ARRAY = 2048 ;
30+ const ALWAYS_ARRAY = 2048 ;
3131 public function __construct ($ host = NULL , $ user = NULL , $ password = NULL , $ database = NULL ) {
3232 if (empty ($ host ) && empty ($ user ) && empty ($ password ) && empty ($ database )) {
3333 if (!empty ($ this ->host )) {
@@ -58,8 +58,8 @@ public function __construct($host = NULL, $user = NULL, $password = NULL, $datab
5858 } catch (PDOException $ e ) {
5959 throw new SmysqlException ("(__construct): Can't connect to MySQL: " . $ e ->getMessage ());
6060 }
61- if (! $ this ->connect ->errorCode () && $ this ->connect ) {
62- throw new SmysqlException ("(__construct): Can't select database MySQL " );
61+ if ($ this ->connect ->errorCode () && $ this ->connect ) {
62+ throw new SmysqlException ("(__construct): Can't select database MySQL ( " . $ this -> connect -> errorInfo ()[ 2 ] . " ) " );
6363 $ this ->connect ->close ();
6464 };
6565 }
@@ -71,7 +71,15 @@ public function escape($string) {
7171 };
7272 return $ r ;
7373 };
74- return $ this ->connect ->quote ($ string );
74+ $ quote = $ this ->connect ->quote ($ string );
75+ $ quoteA = str_split ($ quote );
76+ unset($ quoteA [0 ]);
77+ unset($ quoteA [count ($ quoteA )]);
78+ $ quote = NULL ;
79+ foreach ($ quoteA as $ k => $ v ) {
80+ $ quote .= $ v ;
81+ };
82+ return $ quote ;
7583 }
7684
7785 public function reload () {
@@ -82,8 +90,8 @@ public function query($query, $fnc = "Query") {
8290 if (empty ($ this ->db ) && !in_array ($ fnc , ["__construct " , "changeDB " , "dbList " ]))
8391 throw new SmysqlException ("( " . $ fnc . "): No database selected " );
8492 $ this ->result = $ this ->connect ->query ($ query );
85- if ($ this ->connect -> errorCode () )
86- throw new SmysqlException ("( " . $ fnc . "): Error in MySQL: " . $ this ->connect ->errorInfo () . " <strong>SQL command:</strong> " . $ query );
93+ if (! $ this ->result )
94+ throw new SmysqlException ("( " . $ fnc . "): Error in MySQL: " . $ this ->connect ->errorInfo ()[ 2 ] . " <strong>SQL command:</strong> " . $ query );
8795 return $ this ->result ;
8896 }
8997
@@ -147,7 +155,7 @@ public function tableList() {
147155 $ result = $ this ->result ;
148156 $ this ->query ("SHOW TABLES FROM $ this ->db " , "tableList " );
149157 $ r = [];
150- while ($ f = $ this ->fetchArray ( )) {
158+ while ($ f = $ this ->fetch ( FETCH_ARRAY )) {
151159 $ r [] = $ f [0 ];
152160 };
153161 $ this ->result = $ result ;
@@ -163,21 +171,19 @@ public function charset($charset) {
163171 return $ charset ;
164172 }
165173
166- public function fetch ($ id = false , $ flags = 256 ) {
174+ public function fetch ($ flags = 256 , $ id = false ) {
167175 if ($ id ===false )
168- $ return = $ this ->result -> fetch_object () ;
176+ $ id = $ this ->result ;
169177 if (boolval ($ flags & self ::FETCH_OBJECT ))
170178 $ return = $ id ->fetchObject ();
171179 elseif (boolval ($ flags & self ::FETCH_ARRAY ))
172180 $ return = $ id ->fetch ();
173181 elseif (boolval ($ flags & self ::FETCH_ALL )) {
174182 $ return = [];
175- while ($ row = $ this ->fetch ($ id )) {
183+ while ($ row = $ this ->fetch (self :: FETCH_OBJECT , $ id )) {
176184 $ return [] = $ row ;
177185 };
178- }
179- else
180- $ return = $ id ->fetchObject ();
186+ };
181187 return $ return ;
182188 }
183189
@@ -209,15 +215,15 @@ public function select($table, $order = NULL, $cols = ["*"], $flags = 129) {
209215 " , "select " );
210216 }
211217
212- public function selectWhere ($ table , $ array , $ order = NULL , $ cols = ["* " ], $ flags = 129 , $ exists = false ) {
218+ public function selectWhere ($ table , $ array , $ order = NULL , $ cols = ["* " ], $ flags = 129 , $ name = " selectWhere " ) {
213219 $ all = boolval ($ flags & self ::QUERY_ALL );
214220 $ bool = $ this ->getBool ($ array , $ all );
215221 $ colsValue = implode (", " , $ cols );
216222 if (!empty ($ order ))
217- $ order = "ORDER BY ' " . $ order . "' " . (boolval ($ orderType & self ::ORDER_DESC ) ? "DESC " : "ASC " );
223+ $ order = "ORDER BY ' " . $ order . "' " . (boolval ($ flags & self ::ORDER_DESC ) ? "DESC " : "ASC " );
218224 return $ this ->query ("
219225 SELECT $ colsValue FROM ` $ table` WHERE $ bool $ order
220- " , $ exists ? " exists " : " selectWhere " );
226+ " , $ name );
221227 }
222228
223229 public function selectJoin ($ table , $ join , $ array , $ order = NULL , $ cols = ["* " ], $ flags = 133 ) {
@@ -241,9 +247,9 @@ public function selectJoin($table, $join, $array, $order = NULL, $cols = ["*"],
241247 " , "selectJoin " );
242248 }
243249
244- public function exists ($ table , $ array , $ flags = 128 ) {
250+ public function exists ($ table , $ array , $ flags = 129 , $ name = " exists " ) {
245251 $ all = boolval ($ flags & self ::QUERY_ALL );
246- $ this ->selectWhere ($ table , $ array , $ all , NULL , " ASC " , ["* " ], true );
252+ $ this ->selectWhere ($ table , $ array , NULL , ["* " ], $ flags , $ name );
247253 $ noFetch = !$ this ->fetch ();
248254 return !$ noFetch ;
249255 }
@@ -326,21 +332,21 @@ public function change($table, $name, $newname, $type, $lenth, $null, $data = NU
326332 public function selectAll ($ table ) {
327333 $ r = $ this ->result ;
328334 $ this ->select ($ table );
329- $ f = $ this ->fetchAll ( );
335+ $ f = $ this ->fetch ( self :: FETCH_ALL );
330336 $ this ->result = $ r ;
331337 return $ f ;
332338 }
333339
334- public function fetchWhere ($ table , $ bool , $ flags = 128 ) {
340+ public function fetchWhere ($ table , $ bool , $ flags = 129 ) {
335341 $ all = boolval ($ flags & self ::QUERY_ALL );
336342 $ r = $ this ->result ;
337- $ this ->selectWhere ($ table , $ bool , $ all );
343+ $ this ->selectWhere ($ table , $ bool , $ flags );
338344 $ f = $ this ->fetch ();
339345 $ this ->result = $ r ;
340346 return $ f ;
341347 }
342348
343- public function read ($ table , $ bool = [], $ flags = 128 ) {
349+ public function read ($ table , $ bool = [], $ flags = 129 ) {
344350 $ all = boolval ($ flags & self ::QUERY_ALL );
345351 $ r = $ this ->result ;
346352 $ f = new stdClass ();
@@ -349,14 +355,14 @@ public function read($table, $bool = [], $flags = 128) {
349355 $ f = $ this ->selectAll ($ table );
350356 $ this ->result = $ r ;
351357 }
352- elseif (!$ this ->exists ($ table , $ bool , $ all ))
358+ elseif (!$ this ->exists ($ table , $ bool , $ flags , " read " ))
353359 if (boolval ($ flags & self ::ALWAYS_ARRAY ))
354360 return [];
355361 else
356362 return false ;
357363 else {
358364 $ this ->selectWhere ($ table , $ bool , $ all );
359- $ f = $ this ->fetchAll ( );
365+ $ f = $ this ->fetch ( self :: FETCH_ALL );
360366 };
361367 if ($ f ===new stdClass () && !boolval ($ flags & self ::ALWAYS_ARRAY ))
362368 return false ;
@@ -426,34 +432,50 @@ private function getBool($a, $and, $join = false) {
426432 if (is_array ($ v )) {
427433 foreach ($ v as $ k2 => $ v2 ) {
428434 $ col = false ;
429- if ($ v2 [0 ]=="` " && end (str_split ($ v2 ))=="` " )
435+ if ($ v2 [0 ]=="` " && end (str_split ($ v2 ))=="` " ) {
436+ $ va = str_split ($ v );
437+ unset($ va [0 ]);
438+ unset($ va [count ($ va -1 )]);
430439 $ col = true ;
431- $ v3 = $ this ->escape ($ v2 );
440+ };
441+ if (!is_numeric ($ v2 ))
442+ $ v3 = $ this ->escape ($ v2 );
432443 $ r .= "` " . $ this ->escape ($ k ) . "` " ;
433444 if (is_numeric ($ v3 )) {
434445 $ r .= " = " ;
435446 $ v3 = intval ($ v3 );
436447 }
437448 else
438449 $ r .= " LIKE " ;
439- $ r .= ($ join || $ col ) ? "` $ v3` " : "' $ v3' " ;
450+ if (is_numeric ($ v3 ))
451+ $ r .= $ v ;
452+ else
453+ $ r .= ($ join || $ col ) ? "` $ v3` " : "' $ v3' " ;
440454 $ r .= $ and ? " AND " : " OR " ;
441455 };
442456 return rtrim ($ r , $ and ? " AND " : " OR " );
443457 }
444458 else {
445459 $ col = false ;
446- if ($ v [0 ]=="` " && end (str_split ($ v ))=="` " )
460+ if ($ v [0 ]=="` " && end (str_split ($ v ))=="` " ) {
461+ $ va = str_split ($ v );
462+ unset($ va [0 ]);
463+ unset($ va [count ($ va )]);
447464 $ col = true ;
448- $ v = $ this ->escape ($ v );
465+ };
466+ if (!is_numeric ($ v ))
467+ $ v = $ this ->escape ($ v );
449468 $ r .= "` " . $ this ->escape ($ k ) . "` " ;
450469 if (is_numeric ($ v )) {
451470 $ r .= " = " ;
452- $ v = ( int ) $ v ;
471+ $ v = intval ( $ v ) ;
453472 }
454473 else
455474 $ r .= " LIKE " ;
456- $ r .= ($ join || $ col ) ? "` $ v` " : "' $ v' " ;
475+ if (is_numeric ($ v ))
476+ $ r .= $ v ;
477+ else
478+ $ r .= ($ join || $ col ) ? "` $ v` " : "' $ v' " ;
457479 $ r .= $ and ? " AND " : " OR " ;
458480 };
459481 };
@@ -484,40 +506,40 @@ function Smysql($host, $user, $password, $db, &$object = "return") {
484506
485507 if (!function_exists ("mysql_connect " )) {
486508 function mysql_connect ($ h , $ u , $ p , $ db = NULL ) {
487- return new Smysql \ Smysql ($ h , $ u , $ p , $ db );
509+ return new Smysql ($ h , $ u , $ p , $ db );
488510 };
489511 function mysql_select_db ($ c , $ db ) {
490- if ($ c instanceof Smysql \Smysql )
512+ if ($ c instanceof Smysql)
491513 $ c ->changeDB ($ db );
492514 else
493515 trigger_error ("Invalid connection ID " );
494516 };
495517 function mysql_query ($ q , $ c ) {
496- if ($ c instanceof Smysql \Smysql )
518+ if ($ c instanceof Smysql)
497519 return $ c ->query ($ q );
498520 else
499521 trigger_error ("Invalid connection ID " );
500522 };
501523 function mysql_real_escape_string ($ s , $ c ) {
502- if ($ c instanceof Smysql \Smysql )
524+ if ($ c instanceof Smysql)
503525 $ c ->escape ($ s );
504526 else
505527 trigger_error ("Invalid connection ID " );
506528 };
507529 function mysql_fetch_object ($ r , $ c ) {
508- if ($ c instanceof Smysql \Smysql )
530+ if ($ c instanceof Smysql)
509531 return $ c ->fetch ($ r );
510532 else
511533 trigger_error ("Invalid connection ID " );
512534 };
513535 function mysql_fetch_array ($ r , $ c ) {
514- if ($ c instanceof Smysql \Smysql )
536+ if ($ c instanceof Smysql)
515537 return $ c ->fetchArray ($ r );
516538 else
517539 trigger_error ("Invalid connection ID " );
518540 };
519541 function mysql_close ($ c ) {
520- if ($ c instanceof Smysql \Smysql )
542+ if ($ c instanceof Smysql)
521543 $ c ->__destruct ();
522544 else
523545 trigger_error ("Invalid connection ID " );
0 commit comments