mysql.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,557 行 · 第 1/4 页
PHP
1,557 行
<?php// vim: set et ts=4 sw=4 fdm=marker:// +----------------------------------------------------------------------+// | PHP Version 4 |// +----------------------------------------------------------------------+// | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox, |// | Stig. S. Bakken, Lukas Smith |// | All rights reserved. |// +----------------------------------------------------------------------+// | MDB is a merge of PEAR DB and Metabases that provides a unified DB |// | API as well as database abstraction for PHP applications. |// | This LICENSE is in the BSD license style. |// | |// | Redistribution and use in source and binary forms, with or without |// | modification, are permitted provided that the following conditions |// | are met: |// | |// | Redistributions of source code must retain the above copyright |// | notice, this list of conditions and the following disclaimer. |// | |// | Redistributions in binary form must reproduce the above copyright |// | notice, this list of conditions and the following disclaimer in the |// | documentation and/or other materials provided with the distribution. |// | |// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |// | Lukas Smith nor the names of his contributors may be used to endorse |// | or promote products derived from this software without specific prior|// | written permission. |// | |// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |// | POSSIBILITY OF SUCH DAMAGE. |// +----------------------------------------------------------------------+// | Author: Lukas Smith <smith@backendmedia.com> |// +----------------------------------------------------------------------+//// $Id: mysql.php,v 1.80.4.18 2004/04/22 11:31:56 lsmith Exp $//require_once('MDB/Common.php');/** * MDB MySQL driver * * Notes: * - The decimal type fields are emulated with integer fields. * * @package MDB * @category Database * @author Lukas Smith <smith@backendmedia.com> */class MDB_mysql extends MDB_Common{ // {{{ properties var $connection = 0; var $connected_host; var $connected_user; var $connected_password; var $connected_port; var $opened_persistent = ''; var $escape_quotes = "\\"; var $decimal_factor = 1.0; var $highest_fetched_row = array(); var $columns = array(); // MySQL specific class variable var $default_table_type = ''; var $fixed_float = 0; var $dummy_primary_key = 'dummy_primary_key'; // }}} // {{{ constructor /** * Constructor */ function MDB_mysql() { $this->MDB_Common(); $this->phptype = 'mysql'; $this->dbsyntax = 'mysql'; $this->supported['Sequences'] = 1; $this->supported['Indexes'] = 1; $this->supported['AffectedRows'] = 1; $this->supported['Summaryfunctions'] = 1; $this->supported['OrderByText'] = 1; $this->supported['CurrId'] = 1; $this->supported['SelectRowRanges'] = 1; $this->supported['LOBs'] = 1; $this->supported['Replace'] = 1; $this->supported['SubSelects'] = 0; $this->supported['Transactions'] = 0; $this->decimal_factor = pow(10.0, $this->decimal_places); $this->options['DefaultTableType'] = FALSE; $this->options['fixed_float'] = FALSE; $this->errorcode_map = array( 1004 => MDB_ERROR_CANNOT_CREATE, 1005 => MDB_ERROR_CANNOT_CREATE, 1006 => MDB_ERROR_CANNOT_CREATE, 1007 => MDB_ERROR_ALREADY_EXISTS, 1008 => MDB_ERROR_CANNOT_DROP, 1022 => MDB_ERROR_ALREADY_EXISTS, 1046 => MDB_ERROR_NODBSELECTED, 1050 => MDB_ERROR_ALREADY_EXISTS, 1051 => MDB_ERROR_NOSUCHTABLE, 1054 => MDB_ERROR_NOSUCHFIELD, 1062 => MDB_ERROR_ALREADY_EXISTS, 1064 => MDB_ERROR_SYNTAX, 1100 => MDB_ERROR_NOT_LOCKED, 1136 => MDB_ERROR_VALUE_COUNT_ON_ROW, 1146 => MDB_ERROR_NOSUCHTABLE, 1048 => MDB_ERROR_CONSTRAINT, 1213 => MDB_ERROR_DEADLOCK, 1216 => MDB_ERROR_CONSTRAINT, ); } // }}} // {{{ errorNative() /** * Get the native error code of the last error (if any) that * occured on the current connection. * * @access public * * @return int native MySQL error code */ function errorNative() { return(@mysql_errno($this->connection)); } // }}} // {{{ mysqlRaiseError() /** * This method is used to communicate an error and invoke error * callbacks etc. Basically a wrapper for MDB::raiseError * that checks for native error msgs. * * @param integer $errno error code * @param string $message userinfo message * @return object a PEAR error object * @access public * @see PEAR_Error */ function mysqlRaiseError($errno = NULL, $message = NULL) { if ($errno == NULL) { if ($this->connection) { $errno = @mysql_errno($this->connection); } else { $errno = @mysql_errno(); } } if ($this->connection) { $error = @mysql_errno($this->connection); } else { $error = @mysql_error(); } return($this->raiseError($this->errorCode($errno), NULL, NULL, $message, $error)); } // }}} // {{{ quoteIdentifier() /** * Quote a string so it can be safely used as a table or column name * * Quoting style depends on which database driver is being used. * * MySQL can't handle the backtick character (<kbd>`</kbd>) in * table or column names. * * @param string $str identifier name to be quoted * * @return string quoted identifier string * * @access public * @internal */ function quoteIdentifier($str) { return '`' . $str . '`'; } // }}} // {{{ autoCommit() /** * Define whether database changes done on the database be automatically * committed. This function may also implicitly start or end a transaction. * * @param boolean $auto_commit flag that indicates whether the database * changes should be committed right after * executing every query statement. If this * argument is 0 a transaction implicitly * started. Otherwise, if a transaction is * in progress it is ended by committing any * database changes that were pending. * * @access public * * @return mixed MDB_OK on success, a MDB error on failure */ function autoCommit($auto_commit) { $this->debug("AutoCommit: ".($auto_commit ? "On" : "Off")); if (!isset($this->supported['Transactions'])) { return($this->raiseError(MDB_ERROR_UNSUPPORTED, NULL, NULL, 'Auto-commit transactions: transactions are not in use')); } if ($this->auto_commit == $auto_commit) { return(MDB_OK); } if ($this->connection) { if ($auto_commit) { $result = $this->query('COMMIT'); if (MDB::isError($result)) { return($result); } $result = $this->query('SET AUTOCOMMIT = 1'); if (MDB::isError($result)) { return($result); } } else { $result = $this->query('SET AUTOCOMMIT = 0'); if (MDB::isError($result)) { return($result); } } } $this->auto_commit = $auto_commit; $this->in_transaction = !$auto_commit; return(MDB_OK); } // }}} // {{{ commit() /** * Commit the database changes done during a transaction 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 committing the pending changes. * * @access public * * @return mixed MDB_OK on success, a MDB error on failure */ function commit() { $this->debug("Commit Transaction"); if (!isset($this->supported['Transactions'])) { return($this->raiseError(MDB_ERROR_UNSUPPORTED, NULL, NULL, 'Commit transactions: transactions are not in use')); } if ($this->auto_commit) { return($this->raiseError(MDB_ERROR, NULL, NULL, 'Commit transactions: transaction changes are being auto commited')); } return($this->query('COMMIT')); } // }}} // {{{ rollback() /** * Cancel any database changes done during a transaction 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. * * @access public * * @return mixed MDB_OK on success, a MDB error on failure */ function rollback() { $this->debug("Rollback Transaction"); if (!isset($this->supported['Transactions'])) { return($this->raiseError(MDB_ERROR_UNSUPPORTED, NULL, NULL, 'Rollback transactions: transactions are not in use')); } if ($this->auto_commit) { return($this->raiseError(MDB_ERROR, NULL, NULL, 'Rollback transactions: transactions can not be rolled back when changes are auto commited')); } return($this->query('ROLLBACK')); } // }}} // {{{ connect() /** * Connect to the database * * @return TRUE on success, MDB_Error on failure **/ function connect() { $port = (isset($this->port) ? $this->port : ''); if($this->connection != 0) { if (!strcmp($this->connected_host, $this->host) && !strcmp($this->connected_user, $this->user) && !strcmp($this->connected_password, $this->password) && !strcmp($this->connected_port, $port) && $this->opened_persistent == $this->options['persistent']) { return(MDB_OK); } @mysql_close($this->connection); $this->connection = 0; $this->affected_rows = -1; } if(!PEAR::loadExtension($this->phptype)) { return(PEAR::raiseError(NULL, MDB_ERROR_NOT_FOUND, NULL, NULL, 'extension '.$this->phptype.' is not compiled into PHP', 'MDB_Error', TRUE)); } $UseTransactions = $this->getOption('UseTransactions'); if(!MDB::isError($UseTransactions) && $UseTransactions) { $this->supported['Transactions'] = 1; $this->default_table_type = 'BDB'; } else { $this->supported['Transactions'] = 0; $this->default_table_type = ''; } $DefaultTableType = $this->getOption('DefaultTableType'); if(!MDB::isError($DefaultTableType) && $DefaultTableType) { switch($this->default_table_type = strtoupper($DefaultTableType)) { case 'BERKELEYDB': $this->default_table_type = 'BDB'; case 'BDB': case 'INNODB': case 'GEMINI': break; case 'HEAP': case 'ISAM': case 'MERGE': case 'MRG_MYISAM': case 'MYISAM': if(isset($this->supported['Transactions'])) { $this->warnings[] = $DefaultTableType .' is not a transaction-safe default table type'; } break; default: $this->warnings[] = $DefaultTableType .' is not a supported default table type'; } } $this->fixed_float = 30; $function = ($this->options['persistent'] ? 'mysql_pconnect' : 'mysql_connect'); if (!function_exists($function)) { return($this->raiseError(MDB_ERROR_UNSUPPORTED)); } @ini_set('track_errors', TRUE); $this->connection = @$function( $this->host.(!strcmp($port,'') ? '' : ':'.$port), $this->user, $this->password); @ini_restore('track_errors'); if ($this->connection <= 0) { return($this->raiseError(MDB_ERROR_CONNECT_FAILED, NULL, NULL, $php_errormsg)); } if (isset($this->options['fixedfloat'])) { $this->fixed_float = $this->options['fixedfloat'];
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?