⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysqlc.php

📁 太烦了
💻 PHP
📖 第 1 页 / 共 4 页
字号:
                    $dsn = $this->dsn;                    $dsn['database'] = 'mysql';                    if (DB::isError($db = DB::connect($dsn))) {                        return $db;                    }                    $sql = $db->getCol($sql);                    $db->disconnect();                    // XXX Fixme the mysql driver should take care of this                    if (!@mysql_select_db($this->dsn['database'], $this->connection)) {                        return $this->mysqlRaiseError(DB_ERROR_NODBSELECTED);                    }                }                return $sql;            case 'databases':                return 'SHOW DATABASES';            default:                return null;        }    }    // }}}	/* Following functions are redefining of functions available in DB_common.	 * redefined here to enable caching mechanism	 * Vijay Nair	 */    // {{{ 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 string $query  the SQL query     * @param mixed  $params array, string or numeric data to be used in     *                       execution of the statement.  Quantity of items     *                       passed must match quantity of placeholders in     *                       query:  meaning 1 placeholder for non-array     *                       parameters or 1 placeholder per array element.     *     * @return mixed  the returned value of the query.  DB_Error on failure.     *     * @access public     */    function &getOne($query, $params = array())    {        settype($params, 'array');		$qry = $this->prepareFullQuery($query, $params);		$cached_data = $this->checkCache($qry);		if ($cached_data ) {			$val = $cached_data;			if (is_array($val)) {				$val = $val[0];			}			return $val;		}        if (sizeof($params) > 0) {            $sth = $this->prepare($query);            if (DB::isError($sth)) {                return $sth;            }            $res =& $this->execute($sth, $params);            $this->freePrepared($sth);        } else {            $res =& $this->query($query);        }        if (DB::isError($res)) {            return $res;        }        $err = $res->fetchInto($row, DB_FETCHMODE_ORDERED);        $res->free();        if ($err !== DB_OK) {            return $err;        }		$val = $row[0];		if (is_array($val) ){			$val = array_values($val);		}		$this->saveCache($qry, $val);        return $val;    }    // }}}    // {{{ getRow()    /**     * Fetch the first row of data returned from a query     *     * Takes care of doing the query and freeing the results when finished.     *     * @param string $query  the SQL query     * @param array  $params array to be used in execution of the statement.     *                       Quantity of array elements must match quantity     *                       of placeholders in query.  This function does     *                       NOT support scalars.     * @param int    $fetchmode  the fetch mode to use     *     * @return array the first row of results as an array indexed from     *               0, or a DB error code.     *     * @access public     */    function &getRow($query,                     $params = array(),                     $fetchmode = DB_FETCHMODE_DEFAULT)    {        // compat check, the params and fetchmode parameters used to        // have the opposite order        settype($params, 'array');		$qry = $this->prepareFullQuery($query, $params);		$cached_data = $this->checkCache($qry);		if ($cached_data ) {			return $cached_data;		}        if (!is_array($params)) {            if (is_array($fetchmode)) {                if ($params === null) {                    $tmp = DB_FETCHMODE_DEFAULT;                } else {                    $tmp = $params;                }                $params = $fetchmode;                $fetchmode = $tmp;            } elseif ($params !== null) {                $fetchmode = $params;                $params = array();            }        }        if (sizeof($params) > 0) {            $sth = $this->prepare($query);            if (DB::isError($sth)) {                return $sth;            }            $res =& $this->execute($sth, $params);            $this->freePrepared($sth);        } else {            $res =& $this->query($query);        }        if (DB::isError($res)) {            return $res;        }        $err = $res->fetchInto($row, $fetchmode);        $res->free();        if ($err !== DB_OK) {            return $err;        }		$this->saveCache($qry, $row);        return $row;    }    // }}}    // {{{ getCol()    /**     * Fetch a single column from a result set and return it as an     * indexed array     *     * @param string $query  the SQL query     * @param mixed  $col    which column to return (integer [column number,     *                       starting at 0] or string [column name])     * @param mixed  $params array, string or numeric data to be used in     *                       execution of the statement.  Quantity of items     *                       passed must match quantity of placeholders in     *                       query:  meaning 1 placeholder for non-array     *                       parameters or 1 placeholder per array element.     *     * @return array  an indexed array with the data from the first     *                row at index 0, or a DB error code     *     * @see DB_common::query()     * @access public     */    function &getCol($query, $col = 0, $params = array())    {        settype($params, 'array');		$qry = $this->prepareFullQuery($query, $params);		$cached_data = $this->checkCache($qry);		if ($cached_data ) {			return $cached_data;		}        if (sizeof($params) > 0) {            $sth = $this->prepare($query);            if (DB::isError($sth)) {                return $sth;            }            $res =& $this->execute($sth, $params);            $this->freePrepared($sth);        } 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];        }        $res->free();        if (DB::isError($row)) {            $ret = $row;        }		$this->saveCache($qry, $ret);        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     *     * 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:     *     * <pre>     *  ID      TEXT       DATE     * --------------------------------     *  1       'one'      944679408     *  2       'two'      944679408     *  3       'three'    944679408     * </pre>     *     * Then the call getAssoc('SELECT id,text FROM mytable') returns:     * <pre>     *   array(     *     '1' => 'one',     *     '2' => 'two',     *     '3' => 'three',     *   )     * </pre>     *     * ...while the call getAssoc('SELECT id,text,date FROM mytable') returns:     * <pre>     *   array(     *     '1' => array('one', '944679408'),     *     '2' => array('two', '944679408'),     *     '3' => array('three', '944679408')     *   )     * </pre>     *     * If the more than one row occurs with the same value in the     * first column, the last row overwrites all previous ones by     * default.  Use the $group parameter if you don't want to     * overwrite like this.  Example:     *     * <pre>     * getAssoc('SELECT category,id,name FROM mytable', false, null,     *          DB_FETCHMODE_ASSOC, true) returns:     *     *   array(     *     '1' => array(array('id' => '4', 'name' => 'number four'),     *                  array('id' => '6', 'name' => 'number six')     *            ),     *     '9' => array(array('id' => '4', 'name' => 'number four'),     *                  array('id' => '6', 'name' => 'number six')     *            )     *   )     * </pre>     *     * Keep in mind that database functions in PHP usually return string     * values for results regardless of the database's internal type.     *     * @param string  $query  the SQL query     * @param boolean $force_array  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.     * @param mixed   $params array, string or numeric data to be used in     *                        execution of the statement.  Quantity of items     *                        passed must match quantity of placeholders in     *                        query:  meaning 1 placeholder for non-array     *                        parameters or 1 placeholder per array element.     * @param boolean $group  if true, the values of the returned array     *                        is wrapped in another array.  If the same     *                        key value (in the first column) repeats     *                        itself, the values will be appended to     *                        this array instead of overwriting the     *                        existing values.     *     * @return array  associative array with results from the query.     *                DB Error on failure.     *     * @access public     */    function &getAssoc($query, $force_array = false, $params = array(),                       $fetchmode = DB_FETCHMODE_DEFAULT, $group = false)    {        settype($params, 'array');		$qry = $this->prepareFullQuery($query, $params);		$cached_data = $this->checkCache($qry);		if ($cached_data ) {			return $cached_data;		}        if (sizeof($params) > 0) {            $sth = $this->prepare($query);            if (DB::isError($sth)) {                return $sth;            }            $res =& $this->execute($sth, $params);            $this->freePrepared($sth);        } else {            $res =& $this->query($query);        }        if (DB::isError($res)) {            return $res;        }        if ($fetchmode == DB_FETCHMODE_DEFAULT) {            $fetchmode = $this->fetchmode;        }        $cols = $res->numCols();        if ($cols < 2) {            $tmp =& $this->raiseError(DB_ERROR_TRUNCATED);            return $tmp;        }        $results = array();        if ($cols > 2 || $force_array) {            // return array values            // XXX this part can be optimized            if ($fetchmode == DB_FETCHMODE_ASSOC) {                while (is_array($row = $res->fetchRow(DB_FETCHMODE_ASSOC))) {                    reset($row);                    $key = current($row);                    unset($row[key($row)]);                    if ($group) {                        $results[$key][] = $row;                    } else {                        $results[$key] = $row;                    }                }            } elseif ($fetchmode == DB_FETCHMODE_OBJECT) {                while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) {                    $arr = get_object_vars($row);                    $key = current($arr);                    if ($group) {                        $results[$key][] = $row;                    } else {                        $results[$key] = $row;                    }                }            } else {                while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {                    // we shift away the first element to get                    // indices running from 0 again                    $key = array_shift($row);                    if ($group) {                        $results[$key][] = $row;                    } else {                        $results[$key] = $row;                    }                }            }            if (DB::isError($row)) {                $results = $row;            }        } else {            // return scalar values            while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {                if ($group) {                    $results[$row[0]][] = $row[1];                } else {                    $results[$row[0]] = $row[1];                }            }            if (DB::isError($row)) {                $results = $row;            }        }        $res->free();		$this->saveCache($qry, $results);        return $results;    }    // }}}    // {{{ getAll()    /**     * Fetch all the rows returned from a query     *     * @param string $query  the SQL query     * @param array  $params array to be used in execution of the statement.     *                       Quantity of array elements must match quantity     *                       of placeholders in query.  This function does     *                       NOT support scalars.     * @param int    $fetchmode  the fetch mode to use     *     * @return array  an nested array.  DB error on failure.     *     * @access public     */    function &getAll($query,                     $params = array(),                     $fetchmode = DB_FETCHMODE_DEFAULT)    {		$qry = $this->prepareFullQuery($query, $params);		$cached_data = $this->checkCache($qry);		if ($cached_data ) {			return $cached_data;		}        // compat check, the params and fetchmode parameters used to        // have the opposite order        if (!is_array($params)) {            if (is_array($fetchmode)) {                if ($params === null) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -