@@ -92,13 +92,30 @@ private function _reInit(){
9292 public function init ($ params ){
9393 if (is_object ($ params ))$ params =(array )$ params ;
9494
95-
9695 if (!isset ($ params ['engine ' ]) || !in_array ($ params ['engine ' ],$ this ->_allow_engines )){
9796 $ params ['engine ' ]=$ this ->_engine_name ;
9897 }
9998 $ this ->params =$ params ;
10099 $ this ->__setEngine ($ params ['engine ' ]);
101100 $ this ->_key = implode ("| " ,$ params );
101+
102+ if ($ this ->_engine_name =="mysqli " && extension_loaded ('mysqli ' )){
103+ $ this ->engine = new \SlightPHP \DbMysqli ($ this ->params );
104+ }elseif (extension_loaded ('pdo ' )){
105+ $ this ->engine = new \SlightPHP \DbPDO ($ this ->params );
106+ }else {
107+ trigger_error ("pdo and mysqli extension not exists " ,E_USER_ERROR );
108+ return false ;
109+ }
110+ $ this ->engine ->init ($ this ->params );
111+ if ($ this ->engine ->connect ()===false ){
112+ $ this ->error ['code ' ]=$ this ->engine ->errno ();
113+ $ this ->error ['msg ' ]=$ this ->engine ->error ();
114+ if (defined ("DEBUG " )){
115+ trigger_error ("{$ this ->_engine_name } ( " .var_export ($ this ->error ,true ).") " );
116+ }
117+ return false ;
118+ }
102119 return true ;
103120 }
104121 /**
@@ -146,7 +163,7 @@ public function select($table,$condition="",$item="",$groupby="",$orderby="",$le
146163 //TABLE
147164 $ table = $ this ->__array2string ($ table ,true );
148165 //condition
149- $ condiStr = $ this ->__quote ($ condition ,"AND " );
166+ $ condiStr = $ this ->__quote ($ condition ,"AND " , $ params );
150167
151168 if ($ condiStr !="" ){
152169 $ condiStr =" WHERE " .$ condiStr ;
@@ -204,7 +221,7 @@ public function select($table,$condition="",$item="",$groupby="",$orderby="",$le
204221 $ sql ="SELECT $ item FROM ( $ table) $ join $ condiStr $ groupby $ orderby_sql $ limit_sql " ;
205222 $ start = microtime (true );
206223
207- $ result = $ this ->__query ($ sql );
224+ $ result = $ this ->__query ($ sql, false , $ params );
208225 if ($ result !==false ){
209226 $ data = new DbData ;
210227 $ data ->page = (int )$ this ->page ;
@@ -215,7 +232,7 @@ public function select($table,$condition="",$item="",$groupby="",$orderby="",$le
215232 if ($ this ->count ==true ){
216233 if ($ this ->limit >0 ){
217234 $ countsql ="SELECT count(1) totalSize FROM ( $ table) $ join $ condiStr $ groupby " ;
218- $ result_count = $ this ->__query ($ countsql );
235+ $ result_count = $ this ->__query ($ countsql, false , $ params );
219236 if (!empty ($ result_count [0 ])){
220237 $ data ->totalSize = (int )$ result_count [0 ]['totalSize ' ];
221238 $ data ->totalPage = (int )ceil ($ data ->totalSize /$ data ->limit );
@@ -260,13 +277,13 @@ public function selectOne($table,$condition="",$item="",$groupby="",$orderby="",
260277 */
261278 public function update ($ table ,$ condition ,$ item ){
262279 $ table = $ this ->__array2string ($ table );
263- $ value = $ this ->__quote ($ item ,", " );
264- $ condiStr = $ this ->__quote ($ condition ,"AND " );
280+ $ value = $ this ->__quote ($ item ,", " , $ params );
281+ $ condiStr = $ this ->__quote ($ condition ,"AND " , $ params2 );
265282 if ($ condiStr !="" ){
266283 $ condiStr =" WHERE " .$ condiStr ;
267284 }
268285 $ sql ="UPDATE $ table SET $ value $ condiStr " ;
269- return $ this ->__query ($ sql );
286+ return $ this ->__query ($ sql, false , $ this -> merge_params ( $ params , $ params2 ) );
270287 }
271288 /**
272289 * delete
@@ -277,12 +294,15 @@ public function update($table,$condition,$item){
277294 */
278295 public function delete ($ table ,$ condition ){
279296 $ table = $ this ->__array2string ($ table );
280- $ condiStr = $ this ->__quote ($ condition ,"AND " );
297+ $ condiStr = $ this ->__quote ($ condition ,"AND " , $ params );
281298 if ($ condiStr !="" ){
282299 $ condiStr =" WHERE " .$ condiStr ;
283300 }
284301 $ sql ="DELETE FROM $ table $ condiStr " ;
285- return $ this ->__query ($ sql );
302+ return $ this ->__query ($ sql ,false ,$ params );
303+ }
304+ public function escape ($ str ){
305+ return $ this ->engine ->escape ($ str );
286306 }
287307 /**
288308 * insert
@@ -309,14 +329,24 @@ public function insert($table,$item="",$isreplace=false,$isdelayed=false,$update
309329 $ command .=" DELAYED " ;
310330 }
311331
312- $ f = $ this ->__quote ($ item ,", " );
332+ $ f = $ this ->__quote ($ item ,", " , $ params );
313333
314334 $ sql ="$ command INTO $ table SET $ f " ;
315- $ v = $ this ->__quote ($ update ,", " );
335+ $ v = $ this ->__quote ($ update ,", " , $ params2 );
316336 if (!empty ($ v )){
317337 $ sql .="ON DUPLICATE KEY UPDATE $ v " ;
318338 }
319- return $ this ->__query ($ sql );
339+ return $ this ->__query ($ sql ,false ,$ this ->merge_params ($ params ,$ params2 ));
340+ }
341+
342+ /**
343+ * merge array
344+ */
345+ private function merge_params (...$ arr ){
346+ $ arr = array_filter ($ arr ,function ($ var ){
347+ return ($ var && is_array ($ var )) ? true : false ;
348+ });
349+ return array_merge (...$ arr );
320350 }
321351
322352 /**
@@ -326,7 +356,7 @@ public function insert($table,$item="",$isreplace=false,$isdelayed=false,$update
326356 * @return Array $result || Boolean false
327357 */
328358
329- private function __query ($ sql , $ retry =false ){
359+ private function __query ($ sql , $ retry =false , $ params =[] ){
330360 //{{{
331361 //SQL MODE 默认为DELETE,INSERT,REPLACE 或 UPDATE,不需要返回值
332362 $ sql_mode = 1 ;//1.更新模式 2.查询模式 3.插入模式
@@ -346,26 +376,8 @@ private function __query($sql, $retry=false){
346376 if (defined ("DEBUG " )){
347377 trigger_error ("{$ this ->_engine_name } ( $ sql ) " );
348378 }
349- //Connect
350- if (extension_loaded ('pdo ' )){
351- $ this ->engine = new \SlightPHP \DbPDO ($ this ->params );
352- }elseif (extension_loaded ('mysqli ' )){
353- $ this ->engine = new \SlightPHP \DbMysqli ($ this ->params );
354- }else {
355- trigger_error ("pdo and mysqli extension not exists " ,E_USER_ERROR );
356- return false ;
357- }
358- $ this ->engine ->init ($ this ->params );
359- if ($ this ->engine ->connect ()===false ){
360- $ this ->error ['code ' ]=$ this ->engine ->errno ();
361- $ this ->error ['msg ' ]=$ this ->engine ->error ();
362- if (defined ("DEBUG " )){
363- trigger_error ("{$ this ->_engine_name } ( " .var_export ($ this ->error ,true ).") " );
364- }
365- return false ;
366- }
367379
368- $ result = $ this ->engine ->query ($ sql );
380+ $ result = $ this ->engine ->query ($ sql, $ params );
369381
370382 if ($ result ){
371383 if ($ sql_mode ==2 ){//查询模式
@@ -383,7 +395,7 @@ private function __query($sql, $retry=false){
383395
384396 if ($ retry ===false && $ this ->engine ->connectionError ){
385397 $ this ->_reInit ();
386- return $ this ->__query ($ sql ,true );
398+ return $ this ->__query ($ sql ,true , $ params );
387399 }
388400 trigger_error ("DB QUERY ERROR ( " .var_export ($ this ->error ['msg ' ],true )."), CODE( {$ this ->error ['code ' ]}), SQL( {$ sql }) " ,E_USER_WARNING );
389401 return false ;
@@ -397,7 +409,7 @@ public function execute($sql){
397409 return $ this ->__query ($ sql );
398410 }
399411
400- private function __quote ($ condition ,$ split ="AND " ){
412+ private function __quote ($ condition ,$ split ="AND " ,& $ params =[] ){
401413 $ condiStr = "" ;
402414 if (is_array ($ condition ) || is_object ($ condition )){
403415 $ v1 =array ();
@@ -408,8 +420,8 @@ private function __quote($condition,$split="AND"){
408420 $ k = $ this ->__addsqlslashes ($ k );
409421 }
410422 if (!is_null ($ v )){
411- $ v = addslashes ( $ v ) ;
412- $ v1 []="$ k = \" $ v \" " ;
423+ $ params []= $ v ;
424+ $ v1 []="$ k = ? " ;
413425 }else {
414426 $ v1 []="$ k = NULL " ;
415427 }
0 commit comments