mysqli.php

来自「开源邮件管理系统」· PHP 代码 · 共 1,752 行 · 第 1/5 页

PHP
1,752
字号
     * auto-committing is disabled, otherwise it will fail. Therefore, a new     * transaction is implicitly started after committing the pending changes.     *     * @param   string  name of a savepoint to release     * @return  mixed   MDB2_OK on success, a MDB2 error on failure     *     * @access  public     */    function commit($savepoint = null)    {        $this->debug('Committing transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));        if (!$this->in_transaction) {            return $this->raiseError(MDB2_ERROR_INVALID, null, null,                'commit/release savepoint cannot be done changes are auto committed', __FUNCTION__);        }        if (!is_null($savepoint)) {            if (!$this->supports('savepoints')) {                return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,                    'savepoints are not supported', __FUNCTION__);            }            $server_info = $this->getServerVersion();            if (version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '5.0.3', '<')) {                return MDB2_OK;            }            $query = 'RELEASE SAVEPOINT '.$savepoint;            return $this->_doQuery($query, true);        }        if (!$this->supports('transactions')) {            return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,                'transactions are not supported', __FUNCTION__);        }        $result =& $this->_doQuery('COMMIT', true);        if (PEAR::isError($result)) {            return $result;        }        if (!$this->start_transaction) {            $query = 'SET AUTOCOMMIT = 0';            $result =& $this->_doQuery($query, true);            if (PEAR::isError($result)) {                return $result;            }        }        $this->in_transaction = false;        return MDB2_OK;    }    // }}}    // {{{ rollback()    /**     * Cancel any database changes done during a transaction or since a specific     * savepoint that is in progress. This function may only be called when     * auto-committing is disabled, otherwise it will fail. Therefore, a new     * transaction is implicitly started after canceling the pending changes.     *     * @param   string  name of a savepoint to rollback to     * @return  mixed   MDB2_OK on success, a MDB2 error on failure     *     * @access  public     */    function rollback($savepoint = null)    {        $this->debug('Rolling back transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));        if (!$this->in_transaction) {            return $this->raiseError(MDB2_ERROR_INVALID, null, null,                'rollback cannot be done changes are auto committed', __FUNCTION__);        }        if (!is_null($savepoint)) {            if (!$this->supports('savepoints')) {                return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,                    'savepoints are not supported', __FUNCTION__);            }            $query = 'ROLLBACK TO SAVEPOINT '.$savepoint;            return $this->_doQuery($query, true);        }        $query = 'ROLLBACK';        $result =& $this->_doQuery($query, true);        if (PEAR::isError($result)) {            return $result;        }        if (!$this->start_transaction) {            $query = 'SET AUTOCOMMIT = 0';            $result =& $this->_doQuery($query, true);            if (PEAR::isError($result)) {                return $result;            }        }        $this->in_transaction = false;        return MDB2_OK;    }    // }}}    // {{{ function setTransactionIsolation()    /**     * Set the transacton isolation level.     *     * @param   string  standard isolation level     *                  READ UNCOMMITTED (allows dirty reads)     *                  READ COMMITTED (prevents dirty reads)     *                  REPEATABLE READ (prevents nonrepeatable reads)     *                  SERIALIZABLE (prevents phantom reads)     * @return  mixed   MDB2_OK on success, a MDB2 error on failure     *     * @access  public     * @since   2.1.1     */    function setTransactionIsolation($isolation)    {        $this->debug('Setting transaction isolation level', __FUNCTION__, array('is_manip' => true));        if (!$this->supports('transactions')) {            return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,                'transactions are not supported', __FUNCTION__);        }        switch ($isolation) {        case 'READ UNCOMMITTED':        case 'READ COMMITTED':        case 'REPEATABLE READ':        case 'SERIALIZABLE':            break;        default:            return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,                'isolation level is not supported: '.$isolation, __FUNCTION__);        }        $query = "SET SESSION TRANSACTION ISOLATION LEVEL $isolation";        return $this->_doQuery($query, true);    }    // }}}    // {{{ _doConnect()    /**     * do the grunt work of the connect     *     * @return connection on success or MDB2 Error Object on failure     * @access protected     */    function _doConnect($username, $password, $persistent = false)    {        if (!PEAR::loadExtension($this->phptype)) {            return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,                'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__);        }        $connection = @mysqli_init();        if (!empty($this->dsn['charset']) && defined('MYSQLI_SET_CHARSET_NAME')) {            @mysqli_options($connection, MYSQLI_SET_CHARSET_NAME, $this->dsn['charset']);        }        if ($this->options['ssl']) {            @mysqli_ssl_set(                $connection,                empty($this->dsn['key'])    ? null : $this->dsn['key'],                empty($this->dsn['cert'])   ? null : $this->dsn['cert'],                empty($this->dsn['ca'])     ? null : $this->dsn['ca'],                empty($this->dsn['capath']) ? null : $this->dsn['capath'],                empty($this->dsn['cipher']) ? null : $this->dsn['cipher']            );        }        if (!@mysqli_real_connect(            $connection,            $this->dsn['hostspec'],            $username,            $password,            $this->database_name,            $this->dsn['port'],            $this->dsn['socket']        )) {            if (($err = @mysqli_connect_error()) != '') {                return $this->raiseError(null,                    null, null, $err, __FUNCTION__);            } else {                return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null,                    'unable to establish a connection', __FUNCTION__);            }        }        if (!empty($this->dsn['charset']) && !defined('MYSQLI_SET_CHARSET_NAME')) {            $result = $this->setCharset($this->dsn['charset'], $connection);            if (PEAR::isError($result)) {                return $result;            }        }        return $connection;    }    // }}}    // {{{ connect()    /**     * Connect to the database     *     * @return true on success, MDB2 Error Object on failure     */    function connect()    {        if (is_object($this->connection)) {            //if (count(array_diff($this->connected_dsn, $this->dsn)) == 0) {            if (MDB2::areEquals($this->connected_dsn, $this->dsn)) {                return MDB2_OK;            }            $this->connection = 0;        }        $connection = $this->_doConnect(            $this->dsn['username'],            $this->dsn['password']        );        if (PEAR::isError($connection)) {            return $connection;        }        $this->connection = $connection;        $this->connected_dsn = $this->dsn;        $this->connected_database_name = $this->database_name;        $this->dbsyntax = $this->dsn['dbsyntax'] ? $this->dsn['dbsyntax'] : $this->phptype;        $this->_getServerCapabilities();        return MDB2_OK;    }    // }}}    // {{{ setCharset()    /**     * Set the charset on the current connection     *     * @param string    charset (or array(charset, collation))     * @param resource  connection handle     *     * @return true on success, MDB2 Error Object on failure     */    function setCharset($charset, $connection = null)    {        if (is_null($connection)) {            $connection = $this->getConnection();            if (PEAR::isError($connection)) {                return $connection;            }        }        $collation = null;        if (is_array($charset) && 2 == count($charset)) {            $collation = array_pop($charset);            $charset   = array_pop($charset);        }        $client_info = mysqli_get_client_version();        if (OS_WINDOWS && ((40111 > $client_info) ||            ((50000 <= $client_info) && (50006 > $client_info)))        ) {            $query = "SET NAMES '".mysqli_real_escape_string($connection, $charset)."'";            if (!is_null($collation)) {                $query .= " COLLATE '".mysqli_real_escape_string($connection, $collation)."'";            }            return $this->_doQuery($query, true, $connection);        }        if (!$result = mysqli_set_charset($connection, $charset)) {            $err =& $this->raiseError(null, null, null,                'Could not set client character set', __FUNCTION__);            return $err;        }        return $result;    }    // }}}    // {{{ databaseExists()    /**     * check if given database name is exists?     *     * @param string $name    name of the database that should be checked     *     * @return mixed true/false on success, a MDB2 error on failure     * @access public     */    function databaseExists($name)    {        $connection = $this->_doConnect($this->dsn['username'],                                        $this->dsn['password']);        if (PEAR::isError($connection)) {            return $connection;        }        $result = @mysqli_select_db($connection, $name);        @mysqli_close($connection);        return $result;    }    // }}}    // {{{ disconnect()    /**     * Log out and disconnect from the database.     *     * @param  boolean $force if the disconnect should be forced even if the     *                        connection is opened persistently     * @return mixed true on success, false if not connected and error     *                object on error     * @access public     */    function disconnect($force = true)    {        if (is_object($this->connection)) {            if ($this->in_transaction) {                $dsn = $this->dsn;                $database_name = $this->database_name;                $persistent = $this->options['persistent'];                $this->dsn = $this->connected_dsn;                $this->database_name = $this->connected_database_name;                $this->options['persistent'] = $this->opened_persistent;                $this->rollback();                $this->dsn = $dsn;                $this->database_name = $database_name;                $this->options['persistent'] = $persistent;            }            if ($force) {                @mysqli_close($this->connection);            }        }        return parent::disconnect($force);    }    // }}}    // {{{ standaloneQuery()   /**     * execute a query as DBA     *     * @param string $query the SQL query     * @param mixed   $types  array that contains the types of the columns in     *                        the result set     * @param boolean $is_manip  if the query is a manipulation query     * @return mixed MDB2_OK on success, a MDB2 error on failure     * @access public     */    function &standaloneQuery($query, $types = null, $is_manip = false)    {        $user = $this->options['DBA_username']? $this->options['DBA_username'] : $this->dsn['username'];        $pass = $this->options['DBA_password']? $this->options['DBA_password'] : $this->dsn['password'];        $connection = $this->_doConnect($user, $pass);        if (PEAR::isError($connection)) {            return $connection;        }

⌨️ 快捷键说明

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