Php 修改我的代码以使用删除sql查询
有人知道如何修改我的代码以使其适用于我的delete方法吗?我的当前代码仅用于查询和选择数据库中的某些内容,而不用于删除,因为它需要参数Php 修改我的代码以使用删除sql查询,php,mysql,Php,Mysql,有人知道如何修改我的代码以使其适用于我的delete方法吗?我的当前代码仅用于查询和选择数据库中的某些内容,而不用于删除,因为它需要参数[selectNames],该参数决定它将位于*或字段\u name。例如:DB::getInstance()->get('email','TABLE',array('user_id','=',1))类似的东西 当前代码如下所示: public function action($action, $selectName, $table, $where =
[selectNames]
,该参数决定它将位于*
或字段\u 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);
}
如果操作是SELECT
,我也尝试过更改$sql
变量,但我不知道为什么它不起作用。它不会返回任何错误。我已经尝试了我作为一个普通程序员所知道的每一种调试方法。但是我空手而来。您可以修改您的操作
方法,以便它将所有参数作为一个数组接受,例如
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
];
}
请注意,我将您的操作
方法更改为私有
我希望这会有所帮助。您仍然应该以同样的方式调用它:DB::getInstance()->get('*','users',array('id','=','1'))
区别在于操作
方法,而不是get
和delete
方法(我没有更改它们的参数)。我之所以将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
];
}