From ba045361691ca0d828f547d99ee1b195311510fa Mon Sep 17 00:00:00 2001 From: slawkens Date: Mon, 28 Nov 2022 15:53:14 +0100 Subject: [PATCH] $db->select: make $where parameter optional, allows to get all records --- system/libs/pot/OTS_Base_DB.php | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/system/libs/pot/OTS_Base_DB.php b/system/libs/pot/OTS_Base_DB.php index 21902b9c..f97745b4 100644 --- a/system/libs/pot/OTS_Base_DB.php +++ b/system/libs/pot/OTS_Base_DB.php @@ -92,25 +92,38 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB return $ret; } - public function select($table, $where, $limit = null) + public function select($table, $where = [], $limit = null) { $fields = array_keys($where); $values = array_values($where); - $query = 'SELECT * FROM ' . $this->tableName($table) . ' WHERE ('; + $query = 'SELECT * FROM ' . $this->tableName($table); - $count = count($fields); - for ($i = 0; $i < $count; $i++) - $query.= $this->fieldName($fields[$i]).' = '.$this->quote($values[$i]).' AND '; + if (!empty($where)) { + $query .= ' WHERE ('; + + $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)) - $query .=') LIMIT '.$limit.';'; + $query .=' LIMIT '.$limit.';'; else - $query .=');'; + $query .=';'; $query = $this->query($query); - if($query->rowCount() != 1) return false; - return $query->fetch(); + $rowCount = $query->rowCount(); + if ($rowCount <= 0) return false; + else if ($rowCount == 1) { + return $query->fetch(); + } + + return $query->fetchAll(); + } public function insert($table, $data)