From: Chuck Hagenbuch Date: Sun, 30 Nov 2008 05:15:47 +0000 (-0500) Subject: pdo-optimized selectAssoc X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=048e2602ea4cde7582f2b92abbec00eeeeb25034;p=horde.git pdo-optimized selectAssoc --- diff --git a/framework/Db/lib/Horde/Db/Adapter/Pdo/Abstract.php b/framework/Db/lib/Horde/Db/Adapter/Pdo/Abstract.php index 820fe9bd2..1909d2170 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Pdo/Abstract.php +++ b/framework/Db/lib/Horde/Db/Adapter/Pdo/Abstract.php @@ -112,7 +112,7 @@ abstract class Horde_Db_Adapter_Pdo_Abstract extends Horde_Db_Adapter_Abstract /** * Returns an array of the values of the first column in a select: - * select_values("SELECT id FROM companies LIMIT 3") => [1,2,3] + * selectValues("SELECT id FROM companies LIMIT 3") => [1,2,3] * * @param string $sql * @param mixed $arg1 Either an array of bound parameters or a query name. @@ -124,6 +124,22 @@ abstract class Horde_Db_Adapter_Pdo_Abstract extends Horde_Db_Adapter_Abstract return $result ? $result->fetchAll(PDO::FETCH_COLUMN, 0) : array(); } + /** + * Returns an array where the keys are the first column of a select, and the + * values are the second column: + * + * selectAssoc("SELECT id, name FROM companies LIMIT 3") => [1 => 'Ford', 2 => 'GM', 3 => 'Chrysler'] + * + * @param string $sql + * @param mixed $arg1 Either an array of bound parameters or a query name. + * @param string $arg2 If $arg1 contains bound parameters, the query name. + */ + public function selectAssoc($sql, $arg1=null, $arg2=null) + { + $result = $this->execute($sql, $arg1, $arg2); + return $result ? $result->fetchAll(PDO::FETCH_KEY_PAIR) : array(); + } + /*########################################################################## # Quoting