Anyone know how to modify my code to make it work for my delete method? My current code only works for querying and selecting something in a database but not deleting due to the fact that it requires the parameter [selectNames] which is the parameter that decides where it will be * or field_name. E.g: DB::getInstance()->get('email', 'TABLE', array('user_id', '=', 1)); something like that.
Current code is like this:
public function action($action, $selectName, $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} {$selectName} FROM {$table} WHERE {$field} {$operator} ?";
if(!$this->query($sql, array($value))->error()) {
return $this;
}
}
}
return false;
}
public function get($selectName, $table, $where) {
return $this->action('SELECT', $selectName, $table, $where);
}
public function delete($table, $where) {
return $this->action('DELETE', $table, $where);
}
I tried detecting doing and if/else by checking on the action whether it be a select or a delete. The reason as to why it doesn't work is unknown if you guys figure anything out about what's wrong in my current code please let me know:
public function action($action, $selectName, $table, $where = array()) {
if(count($where) === 3){
$operators = array('=', '>', '<', '>=', '<=');
$field = $where[0];
$operator = $where[1];
$value = $where[2];
if($action == 'SELECT'){
if(in_array($operator, $operators)){
$sql = "{$action} {$selectName} FROM {$table} WHERE {$field} {$operator} ?";
if(!this->query($sql, array($value))->error()){
return $this;
}
}
} else {
if(in_array($operator, $operators)){
$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
if(!$this->query($sql, array($value))->error()) {
return $this;
}
}
}
return false;
}
}
I also have tried changing the $sql variable if the action is SELECT but I don't know why it doesn't work. It does not return any errors whatsoever. I've tried every debugging methods that I know of as a moderate programmer. But I came up empty handed.