$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,27 +92,40 @@ 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);
if (!empty($where)) {
$query .= ' WHERE (';
$count = count($fields); $count = count($fields);
for ($i = 0; $i < $count; $i++) for ($i = 0; $i < $count; $i++) {
$query.= $this->fieldName($fields[$i]).' = '.$this->quote($values[$i]).' AND '; $query .= $this->fieldName($fields[$i]) . ' = ' . $this->quote($values[$i]) . ' AND ';
}
$query = substr($query, 0, -4); $query = substr($query, 0, -4);
$query .= ')';
}
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();
if ($rowCount <= 0) return false;
else if ($rowCount == 1) {
return $query->fetch(); return $query->fetch();
} }
return $query->fetchAll();
}
public function insert($table, $data) public function insert($table, $data)
{ {
$fields = array_keys($data); $fields = array_keys($data);