cvs: /php4/pear/DB common.php mysql.php

From: Date: Wed, 08 Dec 1999 13:50:05 +0000
Subject: cvs: /php4/pear/DB common.php mysql.php
Groups: php.version4 
Request: Send a blank email to php-version4+get-7669@lists.php.net to get a copy of this message
ssb Wed Dec 8 08:50:05 1999 EDT Modified files: /php4/pear/DB common.php mysql.php Log: Added some more error defines (CANNOT_CREATE/DELETE/DROP). Folded up the methods in DB.php. Started adding documentation for DB_result methods. Added doOne() doRow() and doAssoc() methods in DB_common. Added numCols() to DB_mysql. Index: php4/pear/DB/common.php diff -u php4/pear/DB/common.php:1.1 php4/pear/DB/common.php:1.2 --- php4/pear/DB/common.php:1.1 Mon Nov 29 10:13:57 1999 +++ php4/pear/DB/common.php Wed Dec 8 08:49:35 1999 @@ -78,6 +78,104 @@ } // }}} + + // {{{ doOne() + + /** + * Fetch the first column of the first row of data returned from + * a query. Takes care of doing the query and freeing the results + * when finished. + * + * @param $query the SQL query + */ + function doOne($query) { + $sth = $this->query($query); + if (DB::isError($sth)) { + return $sth; + } + $row = $sth->fetchRow(); + $sth->free(); + if (DB::isError($row)) { + return $row; + } + return $row[0]; + } + + // }}} + // {{{ doRow() + + /** + * Fetch the first row of data returned from a query. Takes care + * of doing the query and freeing the results when finished. + * + * @param $query the SQL query + * @return array the first row of results as an array indexed from + * 0, or a DB error code. + */ + function doRow($query) { + $sth = $this->query($query); + if (DB::isError($sth)) { + return $sth; + } + $row = $sth->fetchRow(); + $sth->free(); + return $row; + } + + // }}} + // {{{ doAssoc() + + /** + * Fetch the entire result set of a query and return it as an + * associative array using the first column as the key. + * + * @param $query the SQL query + * + * @param $force_array (optional) used only when the query returns + * exactly two columns. If true, the values of the returned array + * will be one-element arrays instead of scalars. + * + * @return array associative array with results from the query. + * If the result set contains more than two columns, the value + * will be an array of the values from column 2-n. If the result + * set contains only two columns, the returned value will be a + * scalar with the value of the second column (unless forced to an + * array with the $force_array parameter). A DB error code is + * returned on errors. If the result set contains fewer than two + * columns, DB_ERROR_TRUNCATED is returned. + */ + function doAssoc($query, $force_array = false) { + $sth = $this->query($query); + if (DB::isError($sth)) { + return $sth; + } + $cols = $sth->numCols(); + if ($cols < 2) { + return DB_ERROR_TRUNCATED; + } + $results = array(); + // XXX the following code can be optimized! + if ($cols > 2 || $force_array) { + // return array values + while (!DB::isError($row = $sth->fetchRow())) { + reset($row); + // we copy the row data into a new array + // to get indices running from 0 again + for ($i = 1; $i < $cols; $i++) { + $results[$row[0]][$i-1] = $row[$i]; + $results[$row[0]][$k - 1] = $v; + } + } + } else { + // return scalar values + while (!DB::isError($row = $sth->fetchRow())) { + $results[$row[0]] = $row[1]; + } + } + } + + // }}} + } // Local variables: Index: php4/pear/DB/mysql.php diff -u php4/pear/DB/mysql.php:1.2 php4/pear/DB/mysql.php:1.3 --- php4/pear/DB/mysql.php:1.2 Tue Dec 7 16:31:09 1999 +++ php4/pear/DB/mysql.php Wed Dec 8 08:49:35 1999 @@ -141,17 +141,37 @@ } // }}} + // {{{ numCols() + function numCols($result) { + $cols = mysql_num_fields($result); + if (!$cols) { + return $this->mapErrorCode(mysql_errno($this->connection)); + } + return $cols; + } + + // }}} // {{{ mapErrorCode() function mapErrorCode($nativecode) { if (!$codemap) { // XXX FIXME $codemap = array( + 1004 => DB_ERROR_CANNOT_CREATE, + 1005 => DB_ERROR_CANNOT_CREATE, + 1006 => DB_ERROR_CANNOT_CREATE, + 1007 => DB_ERROR_ALREADY_EXISTS, + 1008 => DB_ERROR_CANNOT_DROP, 1046 => DB_ERROR_NODBSELECTED ); + } + if ($codemap[$nativecode]) { + return $codemap[$nativecode]; + } else { + php3_error(E_WARNING, "DB_mysql::mapErrorCode: no mapping for $nativecode"); + return false; } - return $codemap[$nativecode]; } // }}}

« previous php.version4 (#7669) next »