i found in the internet this and so far so good with one condition!! but how can i add 2 or more conditions in the 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);
}
i was thinking to create another function like this:
public function get($table, $where1, $where2) {
return $this->action('SELECT *', $table, $where, $where2);
}
the conditions are $where
and $where2
etc... how can i do this?? im aware that i should create new methods action2 and query2 to implement the changes but as i said im trying 5 days now and i tried everything. plz help me and thanks in advance!!
i found the solution but i had to reduce safety.. i only changed this!
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);
}