$db->select: make $where parameter optional, allows to get all records

This commit is contained in:
slawkens 2022-11-28 15:53:14 +01:00
parent c646dc124b
commit ba04536169

View File

@ -92,25 +92,38 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
return $ret; return $ret;
} }
public function select($table, $where, $limit = null) public function select($table, $where = [], $limit = null)
{ {
$fields = array_keys($where); $fields = array_keys($where);
$values = array_values($where); $values = array_values($where);
$query = 'SELECT * FROM ' . $this->tableName($table) . ' WHERE ('; $query = 'SELECT * FROM ' . $this->tableName($table);
$count = count($fields); if (!empty($where)) {
for ($i = 0; $i < $count; $i++) $query .= ' WHERE (';
$query.= $this->fieldName($fields[$i]).' = '.$this->quote($values[$i]).' AND ';
$count = count($fields);
for ($i = 0; $i < $count; $i++) {
$query .= $this->fieldName($fields[$i]) . ' = ' . $this->quote($values[$i]) . ' AND ';
}
$query = substr($query, 0, -4);
$query .= ')';
}
$query = substr($query, 0, -4);
if (isset($limit)) if (isset($limit))
$query .=') LIMIT '.$limit.';'; $query .=' LIMIT '.$limit.';';
else else
$query .=');'; $query .=';';
$query = $this->query($query); $query = $this->query($query);
if($query->rowCount() != 1) return false; $rowCount = $query->rowCount();
return $query->fetch(); if ($rowCount <= 0) return false;
else if ($rowCount == 1) {
return $query->fetch();
}
return $query->fetchAll();
} }
public function insert($table, $data) public function insert($table, $data)