📄 mysqli.php
字号:
<?php/*** @version $Id: mysqli.php 11316 2008-11-27 03:11:24Z ian $* @package Joomla.Framework* @subpackage Database* @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.* @license GNU/GPL, see LICENSE.php* Joomla! is free software. This version may have been modified pursuant* to the GNU General Public License, and as distributed it includes or* is derivative of works licensed under the GNU General Public License or* other free or open source software licenses.* See COPYRIGHT.php for copyright notices and details.*/// Check to ensure this file is within the rest of the frameworkdefined('JPATH_BASE') or die();/** * MySQLi database driver * * @package Joomla.Framework * @subpackage Database * @since 1.0 */class JDatabaseMySQLi extends JDatabase{ /** * The database driver name * * @var string */ var $name = 'mysqli'; /** * The null/zero date string * * @var string */ var $_nullDate = '0000-00-00 00:00:00'; /** * Quote for named objects * * @var string */ var $_nameQuote = '`'; /** * Database object constructor * * @access public * @param array List of options used to configure the connection * @since 1.5 * @see JDatabase */ function __construct( $options ) { $host = array_key_exists('host', $options) ? $options['host'] : 'localhost'; $user = array_key_exists('user', $options) ? $options['user'] : ''; $password = array_key_exists('password',$options) ? $options['password'] : ''; $database = array_key_exists('database',$options) ? $options['database'] : ''; $prefix = array_key_exists('prefix', $options) ? $options['prefix'] : 'jos_'; $select = array_key_exists('select', $options) ? $options['select'] : true; // Unlike mysql_connect(), mysqli_connect() takes the port and socket // as separate arguments. Therefore, we have to extract them from the // host string. $port = NULL; $socket = NULL; $targetSlot = substr( strstr( $host, ":" ), 1 ); if (!empty( $targetSlot )) { // Get the port number or socket name if (is_numeric( $targetSlot )) $port = $targetSlot; else $socket = $targetSlot; // Extract the host name only $host = substr( $host, 0, strlen( $host ) - (strlen( $targetSlot ) + 1) ); // This will take care of the following notation: ":3306" if($host == '') $host = 'localhost'; } // perform a number of fatality checks, then return gracefully if (!function_exists( 'mysqli_connect' )) { $this->_errorNum = 1; $this->_errorMsg = 'The MySQL adapter "mysqli" is not available.'; return; } // connect to the server if (!($this->_resource = @mysqli_connect($host, $user, $password, NULL, $port, $socket))) { $this->_errorNum = 2; $this->_errorMsg = 'Could not connect to MySQL'; return; } // finalize initialization parent::__construct($options); // select the database if ( $select ) { $this->select($database); } } /** * Database object destructor * * @return boolean * @since 1.5 */ function __destruct() { $return = false; if (is_resource($this->_resource)) { $return = mysqli_close($this->_resource); } return $return; } /** * Test to see if the MySQLi connector is available * * @static * @access public * @return boolean True on success, false otherwise. */ function test() { return (function_exists( 'mysqli_connect' )); } /** * Determines if the connection to the server is active. * * @access public * @return boolean * @since 1.5 */ function connected() { return $this->_resource->ping(); } /** * Select a database for use * * @access public * @param string $database * @return boolean True if the database has been successfully selected * @since 1.5 */ function select($database) { if ( ! $database ) { return false; } if ( !mysqli_select_db($this->_resource, $database)) { $this->_errorNum = 3; $this->_errorMsg = 'Could not connect to database'; return false; } // if running mysql 5, set sql-mode to mysql40 - thereby circumventing strict mode problems if ( strpos( $this->getVersion(), '5' ) === 0 ) { $this->setQuery( "SET sql_mode = 'MYSQL40'" ); $this->query(); } return true; } /** * Determines UTF support * * @access public * @return boolean True - UTF is supported */ function hasUTF() { $verParts = explode( '.', $this->getVersion() ); return ($verParts[0] == 5 || ($verParts[0] == 4 && $verParts[1] == 1 && (int)$verParts[2] >= 2)); } /** * Custom settings for UTF support * * @access public */ function setUTF() { mysqli_query( $this->_resource, "SET NAMES 'utf8'" ); } /** * Get a database escaped string * * @param string The string to be escaped * @param boolean Optional parameter to provide extra escaping * @return string * @access public * @abstract */ function getEscaped( $text, $extra = false ) { $result = mysqli_real_escape_string( $this->_resource, $text ); if ($extra) { $result = addcslashes( $result, '%_' ); } return $result; } /** * Execute the query * * @access public * @return mixed A database resource if successful, FALSE if not. */ function query() { if (!is_object($this->_resource)) { return false; } // Take a local copy so that we don't modify the original query and cause issues later $sql = $this->_sql; if ($this->_limit > 0 || $this->_offset > 0) { $sql .= ' LIMIT '.$this->_offset.', '.$this->_limit; } if ($this->_debug) { $this->_ticker++; $this->_log[] = $sql; } $this->_errorNum = 0; $this->_errorMsg = ''; $this->_cursor = mysqli_query( $this->_resource, $sql ); if (!$this->_cursor) { $this->_errorNum = mysqli_errno( $this->_resource ); $this->_errorMsg = mysqli_error( $this->_resource )." SQL=$sql"; if ($this->_debug) { JError::raiseError(500, 'JDatabaseMySQL::query: '.$this->_errorNum.' - '.$this->_errorMsg ); } return false; } return $this->_cursor; } /** * Description * * @access public * @return int The number of affected rows in the previous operation * @since 1.0.5 */ function getAffectedRows() { return mysqli_affected_rows( $this->_resource ); } /** * Execute a batch query * * @access public * @return mixed A database resource if successful, FALSE if not. */ function queryBatch( $abort_on_error=true, $p_transaction_safe = false) { $this->_errorNum = 0; $this->_errorMsg = ''; if ($p_transaction_safe) { $this->_sql = rtrim($this->_sql, "; \t\r\n\0"); $si = $this->getVersion(); preg_match_all( "/(\d+)\.(\d+)\.(\d+)/i", $si, $m ); if ($m[1] >= 4) { $this->_sql = 'START TRANSACTION;' . $this->_sql . '; COMMIT;'; } else if ($m[2] >= 23 && $m[3] >= 19) { $this->_sql = 'BEGIN WORK;' . $this->_sql . '; COMMIT;'; } else if ($m[2] >= 23 && $m[3] >= 17) { $this->_sql = 'BEGIN;' . $this->_sql . '; COMMIT;'; } } $query_split = $this->splitSql($this->_sql); $error = 0; foreach ($query_split as $command_line) { $command_line = trim( $command_line ); if ($command_line != '') { $this->_cursor = mysqli_query( $this->_resource, $command_line ); if ($this->_debug) { $this->_ticker++; $this->_log[] = $command_line; } if (!$this->_cursor) { $error = 1; $this->_errorNum .= mysqli_errno( $this->_resource ) . ' '; $this->_errorMsg .= mysqli_error( $this->_resource )." SQL=$command_line <br />"; if ($abort_on_error) { return $this->_cursor; } } } } return $error ? false : true; } /** * Diagnostic function * * @access public * @return string */ function explain() { $temp = $this->_sql; $this->_sql = "EXPLAIN $this->_sql"; if (!($cur = $this->query())) { return null; } $first = true; $buffer = '<table id="explain-sql">'; $buffer .= '<thead><tr><td colspan="99">'.$this->getQuery().'</td></tr>'; while ($row = mysqli_fetch_assoc( $cur )) { if ($first) { $buffer .= '<tr>'; foreach ($row as $k=>$v) { $buffer .= '<th>'.$k.'</th>'; } $buffer .= '</tr>'; $first = false; } $buffer .= '</thead><tbody><tr>'; foreach ($row as $k=>$v) { $buffer .= '<td>'.$v.'</td>'; } $buffer .= '</tr>'; } $buffer .= '</tbody></table>'; mysqli_free_result( $cur ); $this->_sql = $temp; return $buffer; } /** * Description * * @access public
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -