修改我的代码以使用删除 sql 查询


Modifying my code to work with delete sql query

>有人知道如何修改我的代码以使其适用于我的删除方法吗?我当前的代码仅适用于查询和选择数据库中的某些内容,但不适用于删除,因为它需要参数[selectNames]参数,该参数是决定*field_name位置的参数。例如:DB::getInstance()->get('email', 'TABLE', array('user_id', '=', 1));类似的东西。

当前代码如下所示:

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

我尝试通过检查操作是选择还是删除来检测正在执行和if/else。为什么它不起作用的原因尚不清楚,如果你们弄清楚我当前代码中的问题,请告诉我:

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

如果操作SELECT,我也尝试更改$sql变量,但我不知道为什么它不起作用。它不会返回任何错误。我已经尝试了我作为一个中等程序员所知道的所有调试方法。但我空手而归。

您可以修改action方法,以便它接受所有参数作为数组,例如

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

请注意,我将您的action方法更改为私有。

我希望这有所帮助。