2016-04-14 25 views
-1

eklemek nasıl bu ve şimdiye kadar çok iyi bir durumda bulundu! ama sql deyiminde 2 veya daha fazla koşulu nasıl ekleyebilirim?başka bir durum pdo sql deyimi

public static function getInstance() { 
    if(!isset(self::$_instance)) { 
     self::$_instance = new DB(); 
    } 
    return self::$_instance; 
} 

public function query($sql, $params = array()) { 
    $this->_error = false; 

    if($this->_query = $this->_pdo->prepare($sql)) { 
     $x = 1; 
     if(count($params)) { 
      foreach($params as $param) { 
       $this->_query->bindValue($x, $param); 
       $x++; 
      } 
     } 

     if($this->_query->execute()) { 
      $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ); 
      $this->_count = $this->_query->rowCount(); 
     } else { 
      $this->_error = true; 
     } 
    } 

    return $this; 
} 

public function action($action, $table, $where = array()) { 
    if(count($where) === 3) { 
     $operators = array('=', '>', '<', '>=', '<='); 

     $field = $where[0]; 
     $operator = $where[1]; 
     $value = $where[2]; 

     if(in_array($operator, $operators)) { 
      $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?"; 

      if(!$this->query($sql, array($value))->error()) { 
       return $this; 
      } 
     } 

    } 

    return false; 
} 

public function get($table, $where) { 
    return $this->action('SELECT *', $table, $where); 
} 

ben böyle başka bir fonksiyon oluşturmak için düşünüyordum:

public function get($table, $where1, $where2) { 
    return $this->action('SELECT *', $table, $where, $where2); 
} 

koşullar $where ve $where2 vb vardır ... ben bunu nasıl ?? Değişiklikleri uygulamak için yeni yöntemler action2 ve query2 oluşturmam gerektiğinin farkındayım ama 5 gün denediğimi söyledim ve herşeyi denedim. plz bana yardım ve şimdiden teşekkürler!

+0

Sadece 1 '$ where' parametresi ile tutmak, ancak çok boyutlu bir dizi yapar. Bir şey gibi: [['alan', 'operatör', 'değer'], ['alan', 'operatör', 'değer']] '. Gerçi yöntemleri değiştirmelisin. – Daan

+0

Yöntemleri değiştirmek için sakıncası yok, $ = nerede denedim (dizi(), dizi()) ve $ alan = $ nerede [0] [0] vb ... ama çalışmadı –

+0

% 95 benim projem bu yöntemler! ama bazı sonuçları tahmin ettiğim 3 şartla filtrelemem gerekiyor. –

cevap

-1

Çözümü buldum ama güvenliği azaltmam gerekiyordu .. Sadece bunu değiştirdim!

public function action3($action, $table, $where = array()) { 
    /*if(count($where) === 6) {*/ 
     $operators = array('=', '>', '<', '>=', '<='); 
     $field1 = $where[0]; 
     $operator1 = $where[1]; 
     $value1 = $where[2]; 
     $field2 = $where[3]; 
     $operator2 = $where[4]; 
     $value2 = $where[5]; 

/*if(in_array($operator, $operators)) {*/ $sql = "{$action} FROM {$table} WHERE {$field1} {$operator1} {$value1} AND {$field2} {$operator2} {$value2}"; if(!$this->query($sql, array($value1, $value2))->error()) {

   return $this; 
      } 
    /* }*/ 

    /* }*/ 

    return false; 
} 

public function get3($table, $where) { 
    return $this->action3('SELECT *', $table, $where); 
} 
İlgili konular