1

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.

1 Answer 1

1

You can modify you action method so that it accepts all parameters as an array, e.g.

private function action($params) {
    $action = isset($params['action']) && $params['action'] ? $params['action'] : null;
    $selectName = isset($params['selectName']) && $params['selectName'] ? strtolower($params['selectName']) : '';
    $table = isset($params['table']) && $params['table'] ? $params['table'] : '';
    $where = isset($params['where']) && $params['where'] ? $params['where'] : [];
    // Check for params integrity
    if ($selectName == '' || $table == '' || !in_array($action, ['select', 'update',' delete'])) {
        // Handle an exception here
    }
    // Here all your logic for handling WHERE operators and values comes
    switch ($action) {
        case 'select':
        case 'delete':
            // Your logic for selecting and deleting records
            break;
        case 'update':
            // Just in case you'll need it later
            break;
    }
}

public function get($selectName, $table, $where) {
    return $this->action([
        'action' => 'select',
        'selectName' => $selectName,
        'table' => $table,
        'where' => $where
    ];
}

public function delete($table, $where) {
    return $this->action([
        'action' => 'delete',
        'table' => $table,
        'where' => $where
    ];
}

Please note that I changed your action method to be private.

I hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

You should call it still in the same way: DB::getInstance()->get('*', 'users', array('id', '=', 1')) The difference is in the action method, not in get and delete methods (I didn't change their arguments). The reason I made action method private is that it now accepts parameters in a different way. But as long as it is private, it's not used anywhere in your code, so nothing will break when the way arguments are passed is changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.