【发布时间】:2015-01-22 12:07:07
【问题描述】:
我想根据多个条件从 MySQL 数据库中提取数据,但我的操作函数只允许我根据 1 个条件提取数据。
请帮我修改动作函数,以便我可以使用多个数组来提取数据。
public function query($sql, $params = array()) {
$this->error = false;
if ($this->query = $this->pdo->prepare($sql)) {
$x = 1;
if (count($params)) {
foreach ($params as $param) {
$this->query->bindValue($x, $param);
$x++;
}
}
if ($this->query->execute()) {
$this->results = $this->query->fetchAll(PDO::FETCH_OBJ);
$this->count = $this->query->rowCount();
} else {
$this->error = true;
}
}
return $this;
}
public function action($action, $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} FROM {$table} WHERE {$field} {$operator} ?";
if (!$this->query($sql, array($value))->error()) {
return $this;
}
}
}
return false;
}
public function get($table, $where) {
return $this->action('SELECT *', $table, $where);
}
【问题讨论】:
-
这是你能想到的最糟糕的数据库抽象层设计。
action?当您尝试使用update时,祝您好运。