--- Form.php.3.1.3-4etch3	2008-03-16 00:07:35.000000000 +0100
+++ Form.php	2008-03-18 16:15:29.000000000 +0100
@@ -3265,36 +3265,44 @@
             switch ($db->phptype) {
             case 'mssql':
                 $db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS | DB_PORTABILITY_RTRIM);
                 break;
             default:
                 $db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS);
             }
 
-            $col = &$db->getCol($sql);
-            if (!is_a($col, 'PEAR_Error')) {
+            // Flip the query so we can work with columns instead of rows
+            $cols = &$db->getAll($sql, array(), DB_FETCHMODE_FLIPPED);
+            if (!is_a($cols, 'PEAR_Error')) {
                 require_once 'Horde/Array.php';
-                $values = Horde_Array::combine($col, $col);
+                if (count($cols) == 1) {
+                    // Use values as keys
+                    $values = Horde_Array::combine($cols[0], $cols[0]);
+                } else if (count($cols) == 2) {
+                    // In case of two columns, assume the first contains
+                    // the keys and the second contains the values
+                    $values = Horde_Array::combine($cols[0], $cols[1]);
+                }
             }
         }
         parent::init($values, $prompt);
     }
 
     /**
      * Return info about field type.
      */
     function about()
     {
         return array(
             'name' => _("Database Lookup"),
             'params' => array(
                 'dsn' => array('label' => _("DSN (see http://pear.php.net/manual/en/package.database.db.intro-dsn.php)"),
                                'type'  => 'text'),
-                'sql' => array('label' => _("SQL statement for value lookups"),
+                'sql' => array('label' => _("SQL statement returning a column of values or a column of keys and a column of values"),
                                'type'  => 'text'),
                 'prompt' => array('label' => _("Prompt text"),
                                   'type'  => 'text'))
             );
     }
 
 }