📄 adodb-mysqli.inc.php
字号:
<?php/*V4.54 5 Nov 2004 (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. Set tabs to 8. MySQL code that does not support transactions. Use mysqlt if you need transactions. Requires mysql client. Works on Windows and Unix. 21 October 2003: MySQLi extension implementation by Arjen de Rijke (a.de.rijke@xs4all.nl)Based on adodb 3.40*/ // security - hide paths//if (!defined('ADODB_DIR')) die();if (! defined("_ADODB_MYSQLI_LAYER")) { define("_ADODB_MYSQLI_LAYER", 1 ); // disable adodb extension - currently incompatible. global $ADODB_EXTENSION; $ADODB_EXTENSION = false;class ADODB_mysqli extends ADOConnection { var $databaseType = 'mysqli'; var $dataProvider = 'native'; var $hasInsertID = true; var $hasAffectedRows = true; var $metaTablesSQL = "SHOW TABLES"; var $metaColumnsSQL = "SHOW COLUMNS FROM %s"; var $fmtTimeStamp = "'Y-m-d H:i:s'"; var $hasLimit = true; var $hasMoveFirst = true; var $hasGenID = true; var $isoDates = true; // accepts dates in ISO format var $sysDate = 'CURDATE()'; var $sysTimeStamp = 'NOW()'; var $hasTransactions = false; var $forceNewConnect = false; var $poorAffectedRows = true; var $clientFlags = 0; var $substr = "substring"; var $port = false; var $socket = false; var $_bindInputArray = false; var $nameQuote = '`'; /// string to use to quote identifiers and names function ADODB_mysqli() { if(!extension_loaded("mysqli")) trigger_error("You must have the mysqli extension installed.", E_USER_ERROR); } // returns true or false // To add: parameter int $port, // parameter string $socket function _connect($argHostname = NULL, $argUsername = NULL, $argPassword = NULL, $argDatabasename = NULL, $persist=false) { $this->_connectionID = @mysqli_init(); if (is_null($this->_connectionID)) { // mysqli_init only fails if insufficient memory if ($this->debug) ADOConnection::outp("mysqli_init() failed : " . $this->ErrorMsg()); return false; } // Set connection options // Not implemented now // mysqli_options($this->_connection,,); if (mysqli_real_connect($this->_connectionID, $argHostname, $argUsername, $argPassword, $argDatabasename, $this->port, $this->socket, $this->clientFlags)) { if ($argDatabasename) return $this->SelectDB($argDatabasename); return true; } else { if ($this->debug) ADOConnection::outp("Could't connect : " . $this->ErrorMsg()); return false; } } // returns true or false // How to force a persistent connection function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename) { return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, true); } // When is this used? Close old connection first? // In _connect(), check $this->forceNewConnect? function _nconnect($argHostname, $argUsername, $argPassword, $argDatabasename) { $this->forceNewConnect = true; $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename); } function IfNull( $field, $ifNull ) { return " IFNULL($field, $ifNull) "; // if MySQL } function ServerInfo() { $arr['description'] = $this->GetOne("select version()"); $arr['version'] = ADOConnection::_findvers($arr['description']); return $arr; } function BeginTrans() { if ($this->transOff) return true; $this->transCnt += 1; $this->Execute('SET AUTOCOMMIT=0'); $this->Execute('BEGIN'); return true; } function CommitTrans($ok=true) { if ($this->transOff) return true; if (!$ok) return $this->RollbackTrans(); if ($this->transCnt) $this->transCnt -= 1; $this->Execute('COMMIT'); $this->Execute('SET AUTOCOMMIT=1'); return true; } function RollbackTrans() { if ($this->transOff) return true; if ($this->transCnt) $this->transCnt -= 1; $this->Execute('ROLLBACK'); $this->Execute('SET AUTOCOMMIT=1'); return true; } // if magic quotes disabled, use mysql_real_escape_string() // From readme.htm: // Quotes a string to be sent to the database. The $magic_quotes_enabled // parameter may look funny, but the idea is if you are quoting a // string extracted from a POST/GET variable, then // pass get_magic_quotes_gpc() as the second parameter. This will // ensure that the variable is not quoted twice, once by qstr and once // by the magic_quotes_gpc. // //Eg. $s = $db->qstr(HTTP_GET_VARS['name'],get_magic_quotes_gpc()); function qstr($s, $magic_quotes = false) { if (!$magic_quotes) { if (PHP_VERSION >= 5) return "'" . mysqli_real_escape_string($this->_connectionID, $s) . "'"; if ($this->replaceQuote[0] == '\\') $s = adodb_str_replace(array('\\',"\0"),array('\\\\',"\\\0"),$s); return "'".str_replace("'",$this->replaceQuote,$s)."'"; } // undo magic quotes for " $s = str_replace('\\"','"',$s); return "'$s'"; } function _insertid() { $result = @mysqli_insert_id($this->_connectionID); if ($result == -1){ if ($this->debug) ADOConnection::outp("mysqli_insert_id() failed : " . $this->ErrorMsg()); } return $result; } // Only works for INSERT, UPDATE and DELETE query's function _affectedrows() { $result = @mysqli_affected_rows($this->_connectionID); if ($result == -1) { if ($this->debug) ADOConnection::outp("mysqli_affected_rows() failed : " . $this->ErrorMsg()); } return $result; } // See http://www.mysql.com/doc/M/i/Miscellaneous_functions.html // Reference on Last_Insert_ID on the recommended way to simulate sequences var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);"; var $_genSeqSQL = "create table %s (id int not null)"; var $_genSeq2SQL = "insert into %s values (%s)"; var $_dropSeqSQL = "drop table %s"; function CreateSequence($seqname='adodbseq',$startID=1) { if (empty($this->_genSeqSQL)) return false; $u = strtoupper($seqname); $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname)); if (!$ok) return false; return $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1)); } function GenID($seqname='adodbseq',$startID=1) { // post-nuke sets hasGenID to false if (!$this->hasGenID) return false; $getnext = sprintf($this->_genIDSQL,$seqname); $holdtransOK = $this->_transOK; // save the current status $rs = @$this->Execute($getnext); if (!$rs) { if ($holdtransOK) $this->_transOK = true; //if the status was ok before reset $u = strtoupper($seqname); $this->Execute(sprintf($this->_genSeqSQL,$seqname)); $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1)); $rs = $this->Execute($getnext); } $this->genID = mysqli_insert_id($this->_connectionID); if ($rs) $rs->Close(); return $this->genID; } function &MetaDatabases() { $query = "SHOW DATABASES"; $ret =& $this->Execute($query); return $ret; } function &MetaIndexes ($table, $primary = FALSE) { // save old fetch mode global $ADODB_FETCH_MODE; $save = $ADODB_FETCH_MODE; $ADODB_FETCH_MODE = ADODB_FETCH_NUM; if ($this->fetchMode !== FALSE) { $savem = $this->SetFetchMode(FALSE); } // get index details $rs = $this->Execute(sprintf('SHOW INDEXES FROM %s',$table)); // restore fetchmode if (isset($savem)) { $this->SetFetchMode($savem); } $ADODB_FETCH_MODE = $save; if (!is_object($rs)) { return FALSE; } $indexes = array (); // parse index data into array while ($row = $rs->FetchRow()) { if ($primary == FALSE AND $row[2] == 'PRIMARY') { continue; } if (!isset($indexes[$row[2]])) { $indexes[$row[2]] = array( 'unique' => ($row[1] == 0), 'columns' => array() ); } $indexes[$row[2]]['columns'][$row[3] - 1] = $row[4]; } // sort columns by order in the index foreach ( array_keys ($indexes) as $index ) { ksort ($indexes[$index]['columns']); } return $indexes; } // Format date column in sql string given an input format that understands Y M D function SQLDate($fmt, $col=false) { if (!$col) $col = $this->sysTimeStamp; $s = 'DATE_FORMAT('.$col.",'"; $concat = false; $len = strlen($fmt); for ($i=0; $i < $len; $i++) { $ch = $fmt[$i]; switch($ch) { case 'Y': case 'y': $s .= '%Y'; break; case 'Q': case 'q': $s .= "'),Quarter($col)"; if ($len > $i+1) $s .= ",DATE_FORMAT($col,'"; else $s .= ",('"; $concat = true; break; case 'M': $s .= '%b'; break; case 'm': $s .= '%m'; break; case 'D': case 'd': $s .= '%d'; break; case 'H': $s .= '%H'; break; case 'h': $s .= '%I'; break; case 'i': $s .= '%i'; break; case 's': $s .= '%s'; break; case 'a': case 'A': $s .= '%p'; break; default: if ($ch == '\\') { $i++; $ch = substr($fmt,$i,1); } $s .= $ch; break; } } $s.="')"; if ($concat) $s = "CONCAT($s)"; return $s; } // returns concatenated string // much easier to run "mysqld --ansi" or "mysqld --sql-mode=PIPES_AS_CONCAT" and use || operator function Concat() { $s = ""; $arr = func_get_args(); // suggestion by andrew005@mnogo.ru $s = implode(',',$arr); if (strlen($s) > 0) return "CONCAT($s)"; else return ''; } // dayFraction is a day in floating point function OffsetDate($dayFraction,$date=false) { if (!$date) $date = $this->sysDate; return "from_unixtime(unix_timestamp($date)+($dayFraction)*24*3600)"; } function &MetaColumns($table) { if (!$this->metaColumnsSQL) return false; global $ADODB_FETCH_MODE; $save = $ADODB_FETCH_MODE; $ADODB_FETCH_MODE = ADODB_FETCH_NUM; if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false); $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table)); if (isset($savem)) $this->SetFetchMode($savem); $ADODB_FETCH_MODE = $save; if (!is_object($rs)) return false; $retarr = array(); while (!$rs->EOF) { $fld = new ADOFieldObject(); $fld->name = $rs->fields[0];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -