📄 common.php
字号:
*/
function &query($query) {
$result = $this->simpleQuery($query);
if (DB::isError($result) || $result === DB_OK) {
return $result;
} else {
return new DB_result($this, $result);
}
}
// }}}
// {{{ limitQuery()
function limitQuery($query, $from, $count)
{
$query = $this->modifyLimitQuery($query, $from, $count);
$result = $this->simpleQuery($query);
if (DB::isError($result) || $result === DB_OK) {
return $result;
} else {
$this->limit_from = $from;
$this->limit_count = $count;
return new DB_result($this, $result);
}
}
// }}}
// {{{ getOne()
/**
* 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
* @param $params if supplied, prepare/execute will be used
* with this array as execute parameters
* @access public
*/
function &getOne($query, $params = array())
{
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;
}
$err = $res->fetchInto($row, DB_FETCHMODE_ORDERED);
if ($err !== DB_OK) {
return $err;
}
$res->free();
if (isset($sth)) {
$this->freeResult($sth);
}
return $row[0];
}
// }}}
// {{{ getRow()
/**
* 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
* @param $fetchmode the fetch mode to use
* @param $params array if supplied, prepare/execute will be used
* with this array as execute parameters
* @access public
* @return array the first row of results as an array indexed from
* 0, or a DB error code.
*/
function &getRow($query,
$params = null,
$fetchmode = DB_FETCHMODE_DEFAULT)
{
// 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;
}
$err = $res->fetchInto($row, $fetchmode);
if ($err !== DB_OK) {
return $err;
}
$res->free();
if (isset($sth)) {
$this->freeResult($sth);
}
return $row;
}
// }}}
// {{{ getCol()
/**
* Fetch a single column from a result set and return it as an
* indexed array.
*
* @param $query the SQL query
*
* @param $col which column to return (integer [column number,
* starting at 0] or string [column name])
*
* @param $params array if supplied, prepare/execute will be used
* with this array as execute parameters
* @access public
*
* @return array an indexed array with the data from the first
* row at index 0, or a DB error code.
*/
function &getCol($query, $col = 0, $params = array())
{
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;
}
$fetchmode = is_int($col) ? DB_FETCHMODE_ORDERED : DB_FETCHMODE_ASSOC;
$ret = array();
while (is_array($row = $res->fetchRow($fetchmode))) {
$ret[] = $row[$col];
}
if (DB::isError($row)) {
$ret = $row;
}
$res->free();
if (isset($sth)) {
$this->freeResult($sth);
}
return $ret;
}
// }}}
// {{{ getAssoc()
/**
* 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.
*
* @access public
*
* @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, a DB_ERROR_TRUNCATED error is returned.
*
* For example, if the table "mytable" contains:
*
* ID TEXT DATE
* --------------------------------
* 1 'one' 944679408
* 2 'two' 944679408
* 3 'three' 944679408
*
* Then the call getAssoc('SELECT id,text FROM mytable') returns:
* array(
* '1' => 'one',
* '2' => 'two',
* '3' => 'three',
* )
*
* ...while the call getAssoc('SELECT id,text,date FROM mytable') returns:
* array(
* '1' => array('one', '944679408'),
* '2' => array('two', '944679408'),
* '3' => array('three', '944679408')
* )
*
* Keep in mind that database functions in PHP usually return string
* values for results regardless of the database's internal type.
*/
function &getAssoc($query, $force_array = false, $params = array())
{
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;
}
$cols = $res->numCols();
if ($cols < 2) {
return $this->raiseError(DB_ERROR_TRUNCATED);
}
$results = array();
if ($cols > 2 || $force_array) {
// return array values
// XXX this part can be optimized
while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {
reset($row);
// we copy the row of data into a new array
// to get indices running from 0 again
$results[$row[0]] = array_slice($row, 1);
}
if (DB::isError($row)) {
$results = $row;
}
} else {
// return scalar values
while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {
$results[$row[0]] = $row[1];
}
if (DB::isError($row)) {
$results = $row;
}
}
$res->free();
if (isset($sth)) {
$this->freeResult($sth);
}
return $results;
}
// }}}
// {{{ getAll()
/**
* Fetch all the rows returned from a query.
*
* @param $query the SQL query
* @access public
* @return array an nested array, or a DB error
*/
function &getAll($query,
$params = null,
$fetchmode = DB_FETCHMODE_DEFAULT)
{
// 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);
while (DB_OK === $res->fetchInto($row, $fetchmode)) {
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;
}
// }}}
// {{{ autoCommit()
function autoCommit($onoff=false)
{
return $this->raiseError(DB_ERROR_NOT_CAPABLE);
}
// }}}
// {{{ commit()
function commit()
{
return $this->raiseError(DB_ERROR_NOT_CAPABLE);
}
// }}}
// {{{ rollback()
function rollback()
{
return $this->raiseError(DB_ERROR_NOT_CAPABLE);
}
// }}}
// {{{ numRows()
function numRows($result)
{
return $this->raiseError(DB_ERROR_NOT_CAPABLE);
}
// }}}
// {{{ affectedRows()
function affectedRows()
{
return $this->raiseError(DB_ERROR_NOT_CAPABLE);
}
// }}}
// {{{ errorNative()
function errorNative()
{
return $this->raiseError(DB_ERROR_NOT_CAPABLE);
}
// }}}
// {{{ nextId()
function nextId($seq_name, $ondemand = true)
{
return $this->raiseError(DB_ERROR_NOT_CAPABLE);
}
// }}}
// {{{ createSequence()
function createSequence($seq_name)
{
return $this->raiseError(DB_ERROR_NOT_CAPABLE);
}
// }}}
// {{{ dropSequence()
function dropSequence($seq_name)
{
return $this->raiseError(DB_ERROR_NOT_CAPABLE);
}
// }}}
// {{{ tableInfo()
function tableInfo($result, $mode = null)
{
return $this->raiseError(DB_ERROR_NOT_CAPABLE);
}
// }}}
// {{{ getTables()
function getTables()
{
return $this->getListOf('tables');
}
// }}}
// {{{ getListOf()
function getListOf($type)
{
$sql = $this->getSpecialQuery($type);
if ($sql === null) { // No support
return $this->raiseError(DB_ERROR_UNSUPPORTED);
} elseif (is_int($sql) || DB::isError($sql)) { // Previous error
return $this->raiseError($sql);
} elseif (is_array($sql)) { // Already the result
return $sql;
}
return $this->getCol($sql); // Launch this query
}
// }}}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -