From 8ffd331c803dbf9de49781efcfbe1f25891be312 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 23 Aug 2019 17:49:41 +0200 Subject: [PATCH] Implemented `PreparedStatement::fetchList()` See #3054 --- .../statement/PreparedStatement.class.php | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/wcfsetup/install/files/lib/system/database/statement/PreparedStatement.class.php b/wcfsetup/install/files/lib/system/database/statement/PreparedStatement.class.php index 4abe0333d6..a4b0675b12 100644 --- a/wcfsetup/install/files/lib/system/database/statement/PreparedStatement.class.php +++ b/wcfsetup/install/files/lib/system/database/statement/PreparedStatement.class.php @@ -23,7 +23,7 @@ class PreparedStatement { * database object * @var Database */ - protected $database = null; + protected $database; /** * SQL query parameters @@ -35,7 +35,7 @@ class PreparedStatement { * pdo statement object * @var \PDOStatement */ - protected $pdoStatement = null; + protected $pdoStatement; /** * SQL query @@ -221,6 +221,27 @@ class PreparedStatement { return $map; } + /** + * Returns a one-dimensional list of all rows holding only the value of the specified column. Please see + * `fetchAll()` if you simply want to read all rows into an array. + * + * @param string $column + * @return string[]|int[]|float[] + */ + public function fetchList($column) { + $list = []; + + while ($row = $this->fetchArray()) { + if (!array_key_exists($column, $row)) { + throw new \RuntimeException("The requested column '{$column}' is not contained in the result rows."); + } + + $list[] = $row[$column]; + } + + return $list; + } + /** * Counts number of affected rows by the last sql statement (INSERT, UPDATE or DELETE). * -- 2.20.1