📄 mysql.php
字号:
register_shutdown_function('MDB2_closeOpenTransactions'); } $query = $this->start_transaction ? 'START TRANSACTION' : 'SET AUTOCOMMIT = 1'; $result =& $this->_doQuery($query, true); if (PEAR::isError($result)) { return $result; } $this->in_transaction = true; return MDB2_OK; } // }}} // {{{ commit() /** * Commit the database changes done during a transaction that is in * progress or release a savepoint. This function may only be called when * 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__); } $params = array(); if ($this->dsn['protocol'] && $this->dsn['protocol'] == 'unix') { $params[0] = ':' . $this->dsn['socket']; } else { $params[0] = $this->dsn['hostspec'] ? $this->dsn['hostspec'] : 'localhost'; if ($this->dsn['port']) { $params[0].= ':' . $this->dsn['port']; } } $params[] = $username ? $username : null; $params[] = $password ? $password : null; if (!$persistent) { if (isset($this->dsn['new_link']) && ($this->dsn['new_link'] == 'true' || $this->dsn['new_link'] === true) ) { $params[] = true; } else { $params[] = false; } } if (version_compare(phpversion(), '4.3.0', '>=')) { $params[] = isset($this->dsn['client_flags']) ? $this->dsn['client_flags'] : null; } $connect_function = $persistent ? 'mysql_pconnect' : 'mysql_connect'; $connection = @call_user_func_array($connect_function, $params); if (!$connection) { if (($err = @mysql_error()) != '') { return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, 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'])) { $result = $this->setCharset($this->dsn['charset'], $connection); if (PEAR::isError($result)) { $this->disconnect(false); return $result; } } return $connection; } // }}} // {{{ connect() /** * Connect to the database * * @return MDB2_OK on success, MDB2 Error Object on failure * @access public */ function connect() { if (is_resource($this->connection)) { //if (count(array_diff($this->connected_dsn, $this->dsn)) == 0 if (MDB2::areEquals($this->connected_dsn, $this->dsn) && $this->opened_persistent == $this->options['persistent'] ) { return MDB2_OK; } $this->disconnect(false); } $connection = $this->_doConnect( $this->dsn['username'], $this->dsn['password'], $this->options['persistent'] ); if (PEAR::isError($connection)) { return $connection; } $this->connection = $connection; $this->connected_dsn = $this->dsn; $this->connected_database_name = ''; $this->opened_persistent = $this->options['persistent']; $this->dbsyntax = $this->dsn['dbsyntax'] ? $this->dsn['dbsyntax'] : $this->phptype; if ($this->database_name) { if ($this->database_name != $this->connected_database_name) { if (!@mysql_select_db($this->database_name, $connection)) { $err = $this->raiseError(null, null, null, 'Could not select the database: '.$this->database_name, __FUNCTION__); return $err; } $this->connected_database_name = $this->database_name; } } $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); } $query = "SET NAMES '".mysql_real_escape_string($charset, $connection)."'"; if (!is_null($collation)) { $query .= " COLLATE '".mysqli_real_escape_string($connection, $collation)."'"; } return $this->_doQuery($query, true, $connection); } // }}} // {{{ 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'], $this->options['persistent']); if (PEAR::isError($connection)) { return $connection; } $result = @mysql_select_db($name, $connection); @mysql_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) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -