2016-04-14 9 views
-1

これはインターネットでこれまでのところ見つかっていて、1つの条件でこれほど良い!しかし、SQLステートメントに2つ以上の条件を追加するにはどうすればよいですか?別の条件を追加する方法pdo sql statement

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

私はこのような別の関数を作成することを考えていた:

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

条件が$where$where2などです...私はこれを行うことができますか?私は新しいメソッドaction2とquery2を作成して変更を実装すべきだと認識していますが、私は5日後に試してみて、すべてを試しました。 PLZは私を助け、事前に感謝!!

+0

私はちょうど1 '$ WHERE'パラメータでそれを維持するが、それ多次元配列になるだろう。 '['フィールド'、 '演算子'、 '値']、[フィールド '、'演算子 '、'値 ']]'のようなものです。あなたは方法を変更する必要があります。 – Daan

+0

私は方法を変更する気にしない、私はどこで$ array = array()、array())と$ field = $ [0] [0] etc ...を試したが、それは働いていなかった –

+0

あなたの95%を参照してください私のプロジェクトはこれらの方法です!私は3つの条件でいくつかの結果をフィルタリングする必要があります。 –

答えて

-1

私はこれだけを変更し ..私は解決策を見つけたが、私は、安全性を減らすために持っていました!

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); 
} 
関連する問題