2016-04-14 9 views
-1

Ich fand im Internet das und so weit so gut mit einer Bedingung! aber wie kann ich 2 oder mehr Bedingungen in der SQL-Anweisung hinzufügen?wie eine weitere Bedingung pdo sql Aussage hinzufügen

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); 
} 

Ich dachte eine andere Funktion wie diese zu erstellen:

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

die Bedingungen sind $where und $where2 etc ... wie kann ich das tun ?? Ich bin mir bewusst, dass ich neue Methoden action2 und query2 erstellen sollte, um die Änderungen zu implementieren, aber wie gesagt, ich versuche jetzt 5 Tage und ich habe alles versucht. Bitte hilf mir und danke im Voraus !!

+0

würde ich es einfach halten mit 1 '$ where' Parameter, aber es ist ein mehrdimensionales Array machen. So etwas wie: '[['Feld', 'Operator', 'Wert'], ['Feld', 'Operator', 'Wert']]' '. Sie müssen die Methoden jedoch ändern. – Daan

+0

ich habe nichts dagegen, die Methoden zu ändern, ich versuchte $ wo = array (array(), array()) und $ field = $ wo [0] [0] etc ... aber es hat nicht funktioniert –

+0

Sie sehen 95% von Mein Projekt ist diese Methode! aber ich muss einige Ergebnisse mit 3 Bedingungen filtern, die ich vermute. –

Antwort

-1

ich fand die Lösung aber ich musste die Sicherheit reduzieren .. ich habe das nur geändert!

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); 
} 
Verwandte Themen