diff --git a/htdocs/core/db/DoliDB.class.php b/htdocs/core/db/DoliDB.class.php index 3b1184c7f85..90cb227db73 100644 --- a/htdocs/core/db/DoliDB.class.php +++ b/htdocs/core/db/DoliDB.class.php @@ -381,10 +381,11 @@ abstract class DoliDB implements Database { $sql .= ' LIMIT 1'; - $res = $this->query($sql); - if ($res) { - $obj = $this->fetch_object($res); + $resql = $this->query($sql); + if ($resql) { + $obj = $this->fetch_object($resql); if ($obj) { + $this->free($resql); return $obj; } else { return 0; @@ -395,24 +396,28 @@ abstract class DoliDB implements Database } /** - * Return all results from query as an array of objects - * Note : This method executes a given SQL query and retrieves all row of results as an array of objects. It should only be used with SELECT queries - * be careful with this method use it only with some limit of results to avoid performances loss. + * Return all results from query as an array of objects. Using this is a bad practice and is discouraged. + * Note : It should only be used with SELECT queries and with a limit. If you are not able to defined/know what can be the limit, it + * just means this function is not what you need. Do not use it. * - * @param string $sql The sql query string - * @return bool|array Result - * @deprecated + * @param string $sql The sql query string. Must end with "... LIMIT x" + * @return bool|array Result */ public function getRows($sql) { - $res = $this->query($sql); - if ($res) { + if (! preg_match('/LIMIT \d+$/', $sql)) { + return false; + } + + $resql = $this->query($sql); + if ($resql) { $results = array(); - if ($this->num_rows($res) > 0) { - while ($obj = $this->fetch_object($res)) { + if ($this->num_rows($resql) > 0) { + while ($obj = $this->fetch_object($resql)) { $results[] = $obj; } } + $this->free($resql); return $results; }