修改我的代码以使用delete sql query

时间:2015-03-15 12:08:18

标签: php mysql

任何人都知道如何修改我的代码以使其适用于我的删除方法?我当前的代码仅适用于查询和选择数据库中的某些内容,但不能删除,因为它需要参数[selectNames],该参数决定了*或{{1}的位置}。例如:field_name类似的东西。

目前的代码是这样的:

DB::getInstance()->get('email', 'TABLE', array('user_id', '=', 1));

我尝试通过检查操作是否为select或delete来检测执行和 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; } } ,我也尝试更改$sql变量,但我不知道为什么它不起作用。它不会返回任何错误。我已经尝试了所有我熟悉的调试方法作为一个温和的程序员。但我空手而归。

1 个答案:

答案 0 :(得分:1)

您可以修改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方法更改为私有。

我希望这会有所帮助。