My contribute do DB/common.php - getMany()

From: Date: Wed, 10 Jul 2002 10:25:08 +0000
Subject: My contribute do DB/common.php - getMany()
References: 1 2 3  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-7624@lists.php.net to get a copy of this message
Hi folks, I was inspired simplicity of Python my_db module for paging results from db, so I decided to write similar easy-to-use function for PHP. This function should be added to DB/commom.php of PEAR. It is based on getAll() method from DB/common.php but it has some advantages. Check examples below. My function could overwrite older getAll() code or could be added as another method with different name: getMany(). Because I have no writing rights to CVS root I include my code below. // }}} // {{{ getMany() /** * Fetch chosen part of the rows returned from a query. * (usefull for paging results from database) * * @param int $from first row, note: first row is 0, not 1 * @param int $limit number of rows to return * @param string $query the SQL query * * @param array $params array if supplied, prepare/execute will be used * with this array as execute parameters * @param integer $fetchmode the fetch mode to use * * @access public * @return array an nested array, or a DB error * * @author Jaroslaw Zabiello <webmaster@watchtower.org.pl> * * Examples: * $db = new MyDB; // MyDB overloads DB_mssql or DB_mysql or another class * $sql = "SELECT * FROM table"; * * // the same as $db->getAll($sql): * print_r($db->getMany($sql)); * * // prints all rows from 4 record: * print_r($db->getMany($sql, 3)); * * // prints 5 rows from 4 record: * print_r($db->getMany($sql, 3, 5)); */ function &getMany($query, $from=null, $limit = null, $params = null, $fetchmode = DB_FETCHMODE_DEFAULT) { if (!isset($from) and !isset($limit)) { return $this->getAll($query, $params, $fetchmode); } // compat check, the params and fetchmode parameters used to // have the opposite order if (!is_array($params)) { if (is_array($fetchmode)) { $tmp = $params; $params = $fetchmode; $fetchmode = $tmp; } elseif ($params !== null) { $fetchmode = $params; $params = null; } } $params = (empty($params)) ? array() : $params; $fetchmode = (empty($fetchmode)) ? DB_FETCHMODE_DEFAULT : $fetchmode; settype($params, "array"); if (sizeof($params) > 0) { $sth = $this->prepare($query); if (DB::isError($sth)) { return $sth; } $res = $this->execute($sth, $params); } else { $res = $this->query($query); } if (DB::isError($res)) { return $res; } $results = array(); $this->pushErrorHandling(PEAR_ERROR_RETURN); if (!isset($limit)) { // all remain rows from choosen point ($from) while ($row = $res->fetchRow($fetchmode, $from++)) { if ($fetchmode & DB_FETCHMODE_FLIPPED) { foreach ($row as $key => $val) { $results[$key][] = $val; } } else { $results[] = $row; } } } else { // only rows from $from to $from+$limit $max = $from + $limit; while ($row = $res->fetchRow($fetchmode, $from++) and $from <= $max) { if ($fetchmode & DB_FETCHMODE_FLIPPED) { foreach ($row as $key => $val) { $results[$key][] = $val; } } else { $results[] = $row; } } } $this->popErrorHandling(); $res->free(); if (isset($sth)) { $this->freeResult($sth); } if (DB::isError($row)) { return $this->raiseError($row); } return $results; }

« previous php.pear.dev (#7624) next »