PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/sample-php/lib/adodb/drivers/adodb-mysqli.inc.php

http://scriptz.googlecode.com/
PHP | 1131 lines | 835 code | 164 blank | 132 comment | 130 complexity | 66ab9a149beebd25d2180e68bdd16753 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. V4.96 24 Sept 2007 (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved.
  4. Released under both BSD license and Lesser GPL library license.
  5. Whenever there is any discrepancy between the two licenses,
  6. the BSD license will take precedence.
  7. Set tabs to 8.
  8. MySQL code that does not support transactions. Use mysqlt if you need transactions.
  9. Requires mysql client. Works on Windows and Unix.
  10. 21 October 2003: MySQLi extension implementation by Arjen de Rijke (a.de.rijke@xs4all.nl)
  11. Based on adodb 3.40
  12. */
  13. // security - hide paths
  14. if (!defined('ADODB_DIR')) die();
  15. if (! defined("_ADODB_MYSQLI_LAYER")) {
  16. define("_ADODB_MYSQLI_LAYER", 1 );
  17. // PHP5 compat...
  18. if (! defined("MYSQLI_BINARY_FLAG")) define("MYSQLI_BINARY_FLAG", 128);
  19. if (!defined('MYSQLI_READ_DEFAULT_GROUP')) define('MYSQLI_READ_DEFAULT_GROUP',1);
  20. // disable adodb extension - currently incompatible.
  21. global $ADODB_EXTENSION; $ADODB_EXTENSION = false;
  22. class ADODB_mysqli extends ADOConnection {
  23. var $databaseType = 'mysqli';
  24. var $dataProvider = 'native';
  25. var $hasInsertID = true;
  26. var $hasAffectedRows = true;
  27. var $metaTablesSQL = "SHOW TABLES";
  28. var $metaColumnsSQL = "SHOW COLUMNS FROM `%s`";
  29. var $fmtTimeStamp = "'Y-m-d H:i:s'";
  30. var $hasLimit = true;
  31. var $hasMoveFirst = true;
  32. var $hasGenID = true;
  33. var $isoDates = true; // accepts dates in ISO format
  34. var $sysDate = 'CURDATE()';
  35. var $sysTimeStamp = 'NOW()';
  36. var $hasTransactions = true;
  37. var $forceNewConnect = false;
  38. var $poorAffectedRows = true;
  39. var $clientFlags = 0;
  40. var $substr = "substring";
  41. var $port = false;
  42. var $socket = false;
  43. var $_bindInputArray = false;
  44. var $nameQuote = '`'; /// string to use to quote identifiers and names
  45. var $optionFlags = array(array(MYSQLI_READ_DEFAULT_GROUP,0));
  46. var $arrayClass = 'ADORecordSet_array_mysqli';
  47. function ADODB_mysqli()
  48. {
  49. // if(!extension_loaded("mysqli"))
  50. ;//trigger_error("You must have the mysqli extension installed.", E_USER_ERROR);
  51. }
  52. function SetTransactionMode( $transaction_mode )
  53. {
  54. $this->_transmode = $transaction_mode;
  55. if (empty($transaction_mode)) {
  56. $this->Execute('SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ');
  57. return;
  58. }
  59. if (!stristr($transaction_mode,'isolation')) $transaction_mode = 'ISOLATION LEVEL '.$transaction_mode;
  60. $this->Execute("SET SESSION TRANSACTION ".$transaction_mode);
  61. }
  62. // returns true or false
  63. // To add: parameter int $port,
  64. // parameter string $socket
  65. function _connect($argHostname = NULL,
  66. $argUsername = NULL,
  67. $argPassword = NULL,
  68. $argDatabasename = NULL, $persist=false)
  69. {
  70. if(!extension_loaded("mysqli")) {
  71. return null;
  72. }
  73. $this->_connectionID = @mysqli_init();
  74. if (is_null($this->_connectionID)) {
  75. // mysqli_init only fails if insufficient memory
  76. if ($this->debug)
  77. ADOConnection::outp("mysqli_init() failed : " . $this->ErrorMsg());
  78. return false;
  79. }
  80. /*
  81. I suggest a simple fix which would enable adodb and mysqli driver to
  82. read connection options from the standard mysql configuration file
  83. /etc/my.cnf - "Bastien Duclaux" <bduclaux#yahoo.com>
  84. */
  85. foreach($this->optionFlags as $arr) {
  86. mysqli_options($this->_connectionID,$arr[0],$arr[1]);
  87. }
  88. #if (!empty($this->port)) $argHostname .= ":".$this->port;
  89. $ok = mysqli_real_connect($this->_connectionID,
  90. $argHostname,
  91. $argUsername,
  92. $argPassword,
  93. $argDatabasename,
  94. $this->port,
  95. $this->socket,
  96. $this->clientFlags);
  97. if ($ok) {
  98. if ($argDatabasename) return $this->SelectDB($argDatabasename);
  99. return true;
  100. } else {
  101. if ($this->debug)
  102. ADOConnection::outp("Could't connect : " . $this->ErrorMsg());
  103. return false;
  104. }
  105. }
  106. // returns true or false
  107. // How to force a persistent connection
  108. function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  109. {
  110. return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, true);
  111. }
  112. // When is this used? Close old connection first?
  113. // In _connect(), check $this->forceNewConnect?
  114. function _nconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  115. {
  116. $this->forceNewConnect = true;
  117. return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename);
  118. }
  119. function IfNull( $field, $ifNull )
  120. {
  121. return " IFNULL($field, $ifNull) "; // if MySQL
  122. }
  123. // do not use $ADODB_COUNTRECS
  124. function GetOne($sql,$inputarr=false)
  125. {
  126. $ret = false;
  127. $rs = &$this->Execute($sql,$inputarr);
  128. if ($rs) {
  129. if (!$rs->EOF) $ret = reset($rs->fields);
  130. $rs->Close();
  131. }
  132. return $ret;
  133. }
  134. function ServerInfo()
  135. {
  136. $arr['description'] = $this->GetOne("select version()");
  137. $arr['version'] = ADOConnection::_findvers($arr['description']);
  138. return $arr;
  139. }
  140. function BeginTrans()
  141. {
  142. if ($this->transOff) return true;
  143. $this->transCnt += 1;
  144. //$this->Execute('SET AUTOCOMMIT=0');
  145. mysqli_autocommit($this->_connectionID, false);
  146. $this->Execute('BEGIN');
  147. return true;
  148. }
  149. function CommitTrans($ok=true)
  150. {
  151. if ($this->transOff) return true;
  152. if (!$ok) return $this->RollbackTrans();
  153. if ($this->transCnt) $this->transCnt -= 1;
  154. $this->Execute('COMMIT');
  155. //$this->Execute('SET AUTOCOMMIT=1');
  156. mysqli_autocommit($this->_connectionID, true);
  157. return true;
  158. }
  159. function RollbackTrans()
  160. {
  161. if ($this->transOff) return true;
  162. if ($this->transCnt) $this->transCnt -= 1;
  163. $this->Execute('ROLLBACK');
  164. //$this->Execute('SET AUTOCOMMIT=1');
  165. mysqli_autocommit($this->_connectionID, true);
  166. return true;
  167. }
  168. function RowLock($tables,$where='',$flds='1 as adodb_ignore')
  169. {
  170. if ($this->transCnt==0) $this->BeginTrans();
  171. if ($where) $where = ' where '.$where;
  172. $rs =& $this->Execute("select $flds from $tables $where for update");
  173. return !empty($rs);
  174. }
  175. // if magic quotes disabled, use mysql_real_escape_string()
  176. // From readme.htm:
  177. // Quotes a string to be sent to the database. The $magic_quotes_enabled
  178. // parameter may look funny, but the idea is if you are quoting a
  179. // string extracted from a POST/GET variable, then
  180. // pass get_magic_quotes_gpc() as the second parameter. This will
  181. // ensure that the variable is not quoted twice, once by qstr and once
  182. // by the magic_quotes_gpc.
  183. //
  184. //Eg. $s = $db->qstr(_GET['name'],get_magic_quotes_gpc());
  185. function qstr($s, $magic_quotes = false)
  186. {
  187. if (is_null($s)) return 'NULL';
  188. if (!$magic_quotes) {
  189. if (PHP_VERSION >= 5)
  190. return "'" . mysqli_real_escape_string($this->_connectionID, $s) . "'";
  191. if ($this->replaceQuote[0] == '\\')
  192. $s = adodb_str_replace(array('\\',"\0"),array('\\\\',"\\\0"),$s);
  193. return "'".str_replace("'",$this->replaceQuote,$s)."'";
  194. }
  195. // undo magic quotes for "
  196. $s = str_replace('\\"','"',$s);
  197. return "'$s'";
  198. }
  199. function _insertid()
  200. {
  201. $result = @mysqli_insert_id($this->_connectionID);
  202. if ($result == -1){
  203. if ($this->debug) ADOConnection::outp("mysqli_insert_id() failed : " . $this->ErrorMsg());
  204. }
  205. return $result;
  206. }
  207. // Only works for INSERT, UPDATE and DELETE query's
  208. function _affectedrows()
  209. {
  210. $result = @mysqli_affected_rows($this->_connectionID);
  211. if ($result == -1) {
  212. if ($this->debug) ADOConnection::outp("mysqli_affected_rows() failed : " . $this->ErrorMsg());
  213. }
  214. return $result;
  215. }
  216. // See http://www.mysql.com/doc/M/i/Miscellaneous_functions.html
  217. // Reference on Last_Insert_ID on the recommended way to simulate sequences
  218. var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);";
  219. var $_genSeqSQL = "create table %s (id int not null)";
  220. var $_genSeq2SQL = "insert into %s values (%s)";
  221. var $_dropSeqSQL = "drop table %s";
  222. function CreateSequence($seqname='adodbseq',$startID=1)
  223. {
  224. if (empty($this->_genSeqSQL)) return false;
  225. $u = strtoupper($seqname);
  226. $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
  227. if (!$ok) return false;
  228. return $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1));
  229. }
  230. function GenID($seqname='adodbseq',$startID=1)
  231. {
  232. // post-nuke sets hasGenID to false
  233. if (!$this->hasGenID) return false;
  234. $getnext = sprintf($this->_genIDSQL,$seqname);
  235. $holdtransOK = $this->_transOK; // save the current status
  236. $rs = @$this->Execute($getnext);
  237. if (!$rs) {
  238. if ($holdtransOK) $this->_transOK = true; //if the status was ok before reset
  239. $u = strtoupper($seqname);
  240. $this->Execute(sprintf($this->_genSeqSQL,$seqname));
  241. $cnt = $this->GetOne(sprintf($this->_genSeqCountSQL,$seqname));
  242. if (!$cnt) $this->Execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1));
  243. $rs = $this->Execute($getnext);
  244. }
  245. if ($rs) {
  246. $this->genID = mysqli_insert_id($this->_connectionID);
  247. $rs->Close();
  248. } else
  249. $this->genID = 0;
  250. return $this->genID;
  251. }
  252. function &MetaDatabases()
  253. {
  254. $query = "SHOW DATABASES";
  255. $ret =& $this->Execute($query);
  256. if ($ret && is_object($ret)){
  257. $arr = array();
  258. while (!$ret->EOF){
  259. $db = $ret->Fields('Database');
  260. if ($db != 'mysql') $arr[] = $db;
  261. $ret->MoveNext();
  262. }
  263. return $arr;
  264. }
  265. return $ret;
  266. }
  267. function &MetaIndexes ($table, $primary = FALSE)
  268. {
  269. // save old fetch mode
  270. global $ADODB_FETCH_MODE;
  271. $false = false;
  272. $save = $ADODB_FETCH_MODE;
  273. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  274. if ($this->fetchMode !== FALSE) {
  275. $savem = $this->SetFetchMode(FALSE);
  276. }
  277. // get index details
  278. $rs = $this->Execute(sprintf('SHOW INDEXES FROM %s',$table));
  279. // restore fetchmode
  280. if (isset($savem)) {
  281. $this->SetFetchMode($savem);
  282. }
  283. $ADODB_FETCH_MODE = $save;
  284. if (!is_object($rs)) {
  285. return $false;
  286. }
  287. $indexes = array ();
  288. // parse index data into array
  289. while ($row = $rs->FetchRow()) {
  290. if ($primary == FALSE AND $row[2] == 'PRIMARY') {
  291. continue;
  292. }
  293. if (!isset($indexes[$row[2]])) {
  294. $indexes[$row[2]] = array(
  295. 'unique' => ($row[1] == 0),
  296. 'columns' => array()
  297. );
  298. }
  299. $indexes[$row[2]]['columns'][$row[3] - 1] = $row[4];
  300. }
  301. // sort columns by order in the index
  302. foreach ( array_keys ($indexes) as $index )
  303. {
  304. ksort ($indexes[$index]['columns']);
  305. }
  306. return $indexes;
  307. }
  308. // Format date column in sql string given an input format that understands Y M D
  309. function SQLDate($fmt, $col=false)
  310. {
  311. if (!$col) $col = $this->sysTimeStamp;
  312. $s = 'DATE_FORMAT('.$col.",'";
  313. $concat = false;
  314. $len = strlen($fmt);
  315. for ($i=0; $i < $len; $i++) {
  316. $ch = $fmt[$i];
  317. switch($ch) {
  318. case 'Y':
  319. case 'y':
  320. $s .= '%Y';
  321. break;
  322. case 'Q':
  323. case 'q':
  324. $s .= "'),Quarter($col)";
  325. if ($len > $i+1) $s .= ",DATE_FORMAT($col,'";
  326. else $s .= ",('";
  327. $concat = true;
  328. break;
  329. case 'M':
  330. $s .= '%b';
  331. break;
  332. case 'm':
  333. $s .= '%m';
  334. break;
  335. case 'D':
  336. case 'd':
  337. $s .= '%d';
  338. break;
  339. case 'H':
  340. $s .= '%H';
  341. break;
  342. case 'h':
  343. $s .= '%I';
  344. break;
  345. case 'i':
  346. $s .= '%i';
  347. break;
  348. case 's':
  349. $s .= '%s';
  350. break;
  351. case 'a':
  352. case 'A':
  353. $s .= '%p';
  354. break;
  355. case 'w':
  356. $s .= '%w';
  357. break;
  358. case 'l':
  359. $s .= '%W';
  360. break;
  361. default:
  362. if ($ch == '\\') {
  363. $i++;
  364. $ch = substr($fmt,$i,1);
  365. }
  366. $s .= $ch;
  367. break;
  368. }
  369. }
  370. $s.="')";
  371. if ($concat) $s = "CONCAT($s)";
  372. return $s;
  373. }
  374. // returns concatenated string
  375. // much easier to run "mysqld --ansi" or "mysqld --sql-mode=PIPES_AS_CONCAT" and use || operator
  376. function Concat()
  377. {
  378. $s = "";
  379. $arr = func_get_args();
  380. // suggestion by andrew005@mnogo.ru
  381. $s = implode(',',$arr);
  382. if (strlen($s) > 0) return "CONCAT($s)";
  383. else return '';
  384. }
  385. // dayFraction is a day in floating point
  386. function OffsetDate($dayFraction,$date=false)
  387. {
  388. if (!$date) $date = $this->sysDate;
  389. $fraction = $dayFraction * 24 * 3600;
  390. return $date . ' + INTERVAL ' . $fraction.' SECOND';
  391. // return "from_unixtime(unix_timestamp($date)+$fraction)";
  392. }
  393. function &MetaTables($ttype=false,$showSchema=false,$mask=false)
  394. {
  395. $save = $this->metaTablesSQL;
  396. if ($showSchema && is_string($showSchema)) {
  397. $this->metaTablesSQL .= " from $showSchema";
  398. }
  399. if ($mask) {
  400. $mask = $this->qstr($mask);
  401. $this->metaTablesSQL .= " like $mask";
  402. }
  403. $ret =& ADOConnection::MetaTables($ttype,$showSchema);
  404. $this->metaTablesSQL = $save;
  405. return $ret;
  406. }
  407. // "Innox - Juan Carlos Gonzalez" <jgonzalez#innox.com.mx>
  408. function MetaForeignKeys( $table, $owner = FALSE, $upper = FALSE, $associative = FALSE )
  409. {
  410. global $ADODB_FETCH_MODE;
  411. if ($ADODB_FETCH_MODE == ADODB_FETCH_ASSOC || $this->fetchMode == ADODB_FETCH_ASSOC) $associative = true;
  412. if ( !empty($owner) ) {
  413. $table = "$owner.$table";
  414. }
  415. $a_create_table = $this->getRow(sprintf('SHOW CREATE TABLE %s', $table));
  416. if ($associative) $create_sql = $a_create_table["Create Table"];
  417. else $create_sql = $a_create_table[1];
  418. $matches = array();
  419. if (!preg_match_all("/FOREIGN KEY \(`(.*?)`\) REFERENCES `(.*?)` \(`(.*?)`\)/", $create_sql, $matches)) return false;
  420. $foreign_keys = array();
  421. $num_keys = count($matches[0]);
  422. for ( $i = 0; $i < $num_keys; $i ++ ) {
  423. $my_field = explode('`, `', $matches[1][$i]);
  424. $ref_table = $matches[2][$i];
  425. $ref_field = explode('`, `', $matches[3][$i]);
  426. if ( $upper ) {
  427. $ref_table = strtoupper($ref_table);
  428. }
  429. $foreign_keys[$ref_table] = array();
  430. $num_fields = count($my_field);
  431. for ( $j = 0; $j < $num_fields; $j ++ ) {
  432. if ( $associative ) {
  433. $foreign_keys[$ref_table][$ref_field[$j]] = $my_field[$j];
  434. } else {
  435. $foreign_keys[$ref_table][] = "{$my_field[$j]}={$ref_field[$j]}";
  436. }
  437. }
  438. }
  439. return $foreign_keys;
  440. }
  441. function &MetaColumns($table)
  442. {
  443. $false = false;
  444. if (!$this->metaColumnsSQL)
  445. return $false;
  446. global $ADODB_FETCH_MODE;
  447. $save = $ADODB_FETCH_MODE;
  448. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  449. if ($this->fetchMode !== false)
  450. $savem = $this->SetFetchMode(false);
  451. $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
  452. if (isset($savem)) $this->SetFetchMode($savem);
  453. $ADODB_FETCH_MODE = $save;
  454. if (!is_object($rs))
  455. return $false;
  456. $retarr = array();
  457. while (!$rs->EOF) {
  458. $fld = new ADOFieldObject();
  459. $fld->name = $rs->fields[0];
  460. $type = $rs->fields[1];
  461. // split type into type(length):
  462. $fld->scale = null;
  463. if (preg_match("/^(.+)\((\d+),(\d+)/", $type, $query_array)) {
  464. $fld->type = $query_array[1];
  465. $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
  466. $fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1;
  467. } elseif (preg_match("/^(.+)\((\d+)/", $type, $query_array)) {
  468. $fld->type = $query_array[1];
  469. $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1;
  470. } elseif (preg_match("/^(enum)\((.*)\)$/i", $type, $query_array)) {
  471. $fld->type = $query_array[1];
  472. $fld->max_length = max(array_map("strlen",explode(",",$query_array[2]))) - 2; // PHP >= 4.0.6
  473. $fld->max_length = ($fld->max_length == 0 ? 1 : $fld->max_length);
  474. } else {
  475. $fld->type = $type;
  476. $fld->max_length = -1;
  477. }
  478. $fld->not_null = ($rs->fields[2] != 'YES');
  479. $fld->primary_key = ($rs->fields[3] == 'PRI');
  480. $fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false);
  481. $fld->binary = (strpos($type,'blob') !== false);
  482. $fld->unsigned = (strpos($type,'unsigned') !== false);
  483. $fld->zerofill = (strpos($type,'zerofill') !== false);
  484. if (!$fld->binary) {
  485. $d = $rs->fields[4];
  486. if ($d != '' && $d != 'NULL') {
  487. $fld->has_default = true;
  488. $fld->default_value = $d;
  489. } else {
  490. $fld->has_default = false;
  491. }
  492. }
  493. if ($save == ADODB_FETCH_NUM) {
  494. $retarr[] = $fld;
  495. } else {
  496. $retarr[strtoupper($fld->name)] = $fld;
  497. }
  498. $rs->MoveNext();
  499. }
  500. $rs->Close();
  501. return $retarr;
  502. }
  503. // returns true or false
  504. function SelectDB($dbName)
  505. {
  506. // $this->_connectionID = $this->mysqli_resolve_link($this->_connectionID);
  507. $this->database = $dbName;
  508. $this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions
  509. if ($this->_connectionID) {
  510. $result = @mysqli_select_db($this->_connectionID, $dbName);
  511. if (!$result) {
  512. ADOConnection::outp("Select of database " . $dbName . " failed. " . $this->ErrorMsg());
  513. }
  514. return $result;
  515. }
  516. return false;
  517. }
  518. // parameters use PostgreSQL convention, not MySQL
  519. function &SelectLimit($sql,
  520. $nrows = -1,
  521. $offset = -1,
  522. $inputarr = false,
  523. $arg3 = false,
  524. $secs = 0)
  525. {
  526. $offsetStr = ($offset >= 0) ? "$offset," : '';
  527. if ($nrows < 0) $nrows = '18446744073709551615';
  528. if ($secs)
  529. $rs =& $this->CacheExecute($secs, $sql . " LIMIT $offsetStr$nrows" , $inputarr , $arg3);
  530. else
  531. $rs =& $this->Execute($sql . " LIMIT $offsetStr$nrows" , $inputarr , $arg3);
  532. return $rs;
  533. }
  534. function Prepare($sql)
  535. {
  536. return $sql;
  537. $stmt = $this->_connectionID->prepare($sql);
  538. if (!$stmt) {
  539. echo $this->ErrorMsg();
  540. return $sql;
  541. }
  542. return array($sql,$stmt);
  543. }
  544. // returns queryID or false
  545. function _query($sql, $inputarr)
  546. {
  547. global $ADODB_COUNTRECS;
  548. if (is_array($sql)) {
  549. $stmt = $sql[1];
  550. $a = '';
  551. foreach($inputarr as $k => $v) {
  552. if (is_string($v)) $a .= 's';
  553. else if (is_integer($v)) $a .= 'i';
  554. else $a .= 'd';
  555. }
  556. $fnarr = array_merge( array($stmt,$a) , $inputarr);
  557. $ret = call_user_func_array('mysqli_stmt_bind_param',$fnarr);
  558. $ret = mysqli_stmt_execute($stmt);
  559. return $ret;
  560. }
  561. if (!$mysql_res = mysqli_query($this->_connectionID, $sql, ($ADODB_COUNTRECS) ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT)) {
  562. if ($this->debug) ADOConnection::outp("Query: " . $sql . " failed. " . $this->ErrorMsg());
  563. return false;
  564. }
  565. return $mysql_res;
  566. }
  567. /* Returns: the last error message from previous database operation */
  568. function ErrorMsg()
  569. {
  570. if (empty($this->_connectionID))
  571. $this->_errorMsg = @mysqli_connect_error();
  572. else
  573. $this->_errorMsg = @mysqli_error($this->_connectionID);
  574. return $this->_errorMsg;
  575. }
  576. /* Returns: the last error number from previous database operation */
  577. function ErrorNo()
  578. {
  579. if (empty($this->_connectionID))
  580. return @mysqli_connect_errno();
  581. else
  582. return @mysqli_errno($this->_connectionID);
  583. }
  584. // returns true or false
  585. function _close()
  586. {
  587. @mysqli_close($this->_connectionID);
  588. $this->_connectionID = false;
  589. }
  590. /*
  591. * Maximum size of C field
  592. */
  593. function CharMax()
  594. {
  595. return 255;
  596. }
  597. /*
  598. * Maximum size of X field
  599. */
  600. function TextMax()
  601. {
  602. return 4294967295;
  603. }
  604. // this is a set of functions for managing client encoding - very important if the encodings
  605. // of your database and your output target (i.e. HTML) don't match
  606. // for instance, you may have UTF8 database and server it on-site as latin1 etc.
  607. // GetCharSet - get the name of the character set the client is using now
  608. // Under Windows, the functions should work with MySQL 4.1.11 and above, the set of charsets supported
  609. // depends on compile flags of mysql distribution
  610. function GetCharSet()
  611. {
  612. //we will use ADO's builtin property charSet
  613. if (!method_exists($this->_connectionID,'character_set_name'))
  614. return false;
  615. $this->charSet = @$this->_connectionID->character_set_name();
  616. if (!$this->charSet) {
  617. return false;
  618. } else {
  619. return $this->charSet;
  620. }
  621. }
  622. // SetCharSet - switch the client encoding
  623. function SetCharSet($charset_name)
  624. {
  625. if (!method_exists($this->_connectionID,'set_charset'))
  626. return false;
  627. if ($this->charSet !== $charset_name) {
  628. $if = @$this->_connectionID->set_charset($charset_name);
  629. if ($if == "0" & $this->GetCharSet() == $charset_name) {
  630. return true;
  631. } else return false;
  632. } else return true;
  633. }
  634. }
  635. /*--------------------------------------------------------------------------------------
  636. Class Name: Recordset
  637. --------------------------------------------------------------------------------------*/
  638. class ADORecordSet_mysqli extends ADORecordSet{
  639. var $databaseType = "mysqli";
  640. var $canSeek = true;
  641. function ADORecordSet_mysqli($queryID, $mode = false)
  642. {
  643. if ($mode === false)
  644. {
  645. global $ADODB_FETCH_MODE;
  646. $mode = $ADODB_FETCH_MODE;
  647. }
  648. switch ($mode)
  649. {
  650. case ADODB_FETCH_NUM:
  651. $this->fetchMode = MYSQLI_NUM;
  652. break;
  653. case ADODB_FETCH_ASSOC:
  654. $this->fetchMode = MYSQLI_ASSOC;
  655. break;
  656. case ADODB_FETCH_DEFAULT:
  657. case ADODB_FETCH_BOTH:
  658. default:
  659. $this->fetchMode = MYSQLI_BOTH;
  660. break;
  661. }
  662. $this->adodbFetchMode = $mode;
  663. $this->ADORecordSet($queryID);
  664. }
  665. function _initrs()
  666. {
  667. global $ADODB_COUNTRECS;
  668. $this->_numOfRows = $ADODB_COUNTRECS ? @mysqli_num_rows($this->_queryID) : -1;
  669. $this->_numOfFields = @mysqli_num_fields($this->_queryID);
  670. }
  671. /*
  672. 1 = MYSQLI_NOT_NULL_FLAG
  673. 2 = MYSQLI_PRI_KEY_FLAG
  674. 4 = MYSQLI_UNIQUE_KEY_FLAG
  675. 8 = MYSQLI_MULTIPLE_KEY_FLAG
  676. 16 = MYSQLI_BLOB_FLAG
  677. 32 = MYSQLI_UNSIGNED_FLAG
  678. 64 = MYSQLI_ZEROFILL_FLAG
  679. 128 = MYSQLI_BINARY_FLAG
  680. 256 = MYSQLI_ENUM_FLAG
  681. 512 = MYSQLI_AUTO_INCREMENT_FLAG
  682. 1024 = MYSQLI_TIMESTAMP_FLAG
  683. 2048 = MYSQLI_SET_FLAG
  684. 32768 = MYSQLI_NUM_FLAG
  685. 16384 = MYSQLI_PART_KEY_FLAG
  686. 32768 = MYSQLI_GROUP_FLAG
  687. 65536 = MYSQLI_UNIQUE_FLAG
  688. 131072 = MYSQLI_BINCMP_FLAG
  689. */
  690. function &FetchField($fieldOffset = -1)
  691. {
  692. $fieldnr = $fieldOffset;
  693. if ($fieldOffset != -1) {
  694. $fieldOffset = mysqli_field_seek($this->_queryID, $fieldnr);
  695. }
  696. $o = mysqli_fetch_field($this->_queryID);
  697. /* Properties of an ADOFieldObject as set by MetaColumns */
  698. $o->primary_key = $o->flags & MYSQLI_PRI_KEY_FLAG;
  699. $o->not_null = $o->flags & MYSQLI_NOT_NULL_FLAG;
  700. $o->auto_increment = $o->flags & MYSQLI_AUTO_INCREMENT_FLAG;
  701. $o->binary = $o->flags & MYSQLI_BINARY_FLAG;
  702. // $o->blob = $o->flags & MYSQLI_BLOB_FLAG; /* not returned by MetaColumns */
  703. $o->unsigned = $o->flags & MYSQLI_UNSIGNED_FLAG;
  704. return $o;
  705. }
  706. function &GetRowAssoc($upper = true)
  707. {
  708. if ($this->fetchMode == MYSQLI_ASSOC && !$upper)
  709. return $this->fields;
  710. $row =& ADORecordSet::GetRowAssoc($upper);
  711. return $row;
  712. }
  713. /* Use associative array to get fields array */
  714. function Fields($colname)
  715. {
  716. if ($this->fetchMode != MYSQLI_NUM)
  717. return @$this->fields[$colname];
  718. if (!$this->bind) {
  719. $this->bind = array();
  720. for ($i = 0; $i < $this->_numOfFields; $i++) {
  721. $o = $this->FetchField($i);
  722. $this->bind[strtoupper($o->name)] = $i;
  723. }
  724. }
  725. return $this->fields[$this->bind[strtoupper($colname)]];
  726. }
  727. function _seek($row)
  728. {
  729. if ($this->_numOfRows == 0)
  730. return false;
  731. if ($row < 0)
  732. return false;
  733. mysqli_data_seek($this->_queryID, $row);
  734. $this->EOF = false;
  735. return true;
  736. }
  737. // 10% speedup to move MoveNext to child class
  738. // This is the only implementation that works now (23-10-2003).
  739. // Other functions return no or the wrong results.
  740. function MoveNext()
  741. {
  742. if ($this->EOF) return false;
  743. $this->_currentRow++;
  744. $this->fields = @mysqli_fetch_array($this->_queryID,$this->fetchMode);
  745. if (is_array($this->fields)) return true;
  746. $this->EOF = true;
  747. return false;
  748. }
  749. function _fetch()
  750. {
  751. $this->fields = mysqli_fetch_array($this->_queryID,$this->fetchMode);
  752. return is_array($this->fields);
  753. }
  754. function _close()
  755. {
  756. mysqli_free_result($this->_queryID);
  757. $this->_queryID = false;
  758. }
  759. /*
  760. 0 = MYSQLI_TYPE_DECIMAL
  761. 1 = MYSQLI_TYPE_CHAR
  762. 1 = MYSQLI_TYPE_TINY
  763. 2 = MYSQLI_TYPE_SHORT
  764. 3 = MYSQLI_TYPE_LONG
  765. 4 = MYSQLI_TYPE_FLOAT
  766. 5 = MYSQLI_TYPE_DOUBLE
  767. 6 = MYSQLI_TYPE_NULL
  768. 7 = MYSQLI_TYPE_TIMESTAMP
  769. 8 = MYSQLI_TYPE_LONGLONG
  770. 9 = MYSQLI_TYPE_INT24
  771. 10 = MYSQLI_TYPE_DATE
  772. 11 = MYSQLI_TYPE_TIME
  773. 12 = MYSQLI_TYPE_DATETIME
  774. 13 = MYSQLI_TYPE_YEAR
  775. 14 = MYSQLI_TYPE_NEWDATE
  776. 247 = MYSQLI_TYPE_ENUM
  777. 248 = MYSQLI_TYPE_SET
  778. 249 = MYSQLI_TYPE_TINY_BLOB
  779. 250 = MYSQLI_TYPE_MEDIUM_BLOB
  780. 251 = MYSQLI_TYPE_LONG_BLOB
  781. 252 = MYSQLI_TYPE_BLOB
  782. 253 = MYSQLI_TYPE_VAR_STRING
  783. 254 = MYSQLI_TYPE_STRING
  784. 255 = MYSQLI_TYPE_GEOMETRY
  785. */
  786. function MetaType($t, $len = -1, $fieldobj = false)
  787. {
  788. if (is_object($t)) {
  789. $fieldobj = $t;
  790. $t = $fieldobj->type;
  791. $len = $fieldobj->max_length;
  792. }
  793. $len = -1; // mysql max_length is not accurate
  794. switch (strtoupper($t)) {
  795. case 'STRING':
  796. case 'CHAR':
  797. case 'VARCHAR':
  798. case 'TINYBLOB':
  799. case 'TINYTEXT':
  800. case 'ENUM':
  801. case 'SET':
  802. case MYSQLI_TYPE_TINY_BLOB :
  803. case MYSQLI_TYPE_CHAR :
  804. case MYSQLI_TYPE_STRING :
  805. case MYSQLI_TYPE_ENUM :
  806. case MYSQLI_TYPE_SET :
  807. case 253 :
  808. if ($len <= $this->blobSize) return 'C';
  809. case 'TEXT':
  810. case 'LONGTEXT':
  811. case 'MEDIUMTEXT':
  812. return 'X';
  813. // php_mysql extension always returns 'blob' even if 'text'
  814. // so we have to check whether binary...
  815. case 'IMAGE':
  816. case 'LONGBLOB':
  817. case 'BLOB':
  818. case 'MEDIUMBLOB':
  819. case MYSQLI_TYPE_BLOB :
  820. case MYSQLI_TYPE_LONG_BLOB :
  821. case MYSQLI_TYPE_MEDIUM_BLOB :
  822. return !empty($fieldobj->binary) ? 'B' : 'X';
  823. case 'YEAR':
  824. case 'DATE':
  825. case MYSQLI_TYPE_DATE :
  826. case MYSQLI_TYPE_YEAR :
  827. return 'D';
  828. case 'TIME':
  829. case 'DATETIME':
  830. case 'TIMESTAMP':
  831. case MYSQLI_TYPE_DATETIME :
  832. case MYSQLI_TYPE_NEWDATE :
  833. case MYSQLI_TYPE_TIME :
  834. case MYSQLI_TYPE_TIMESTAMP :
  835. return 'T';
  836. case 'INT':
  837. case 'INTEGER':
  838. case 'BIGINT':
  839. case 'TINYINT':
  840. case 'MEDIUMINT':
  841. case 'SMALLINT':
  842. case MYSQLI_TYPE_INT24 :
  843. case MYSQLI_TYPE_LONG :
  844. case MYSQLI_TYPE_LONGLONG :
  845. case MYSQLI_TYPE_SHORT :
  846. case MYSQLI_TYPE_TINY :
  847. if (!empty($fieldobj->primary_key)) return 'R';
  848. return 'I';
  849. // Added floating-point types
  850. // Maybe not necessery.
  851. case 'FLOAT':
  852. case 'DOUBLE':
  853. // case 'DOUBLE PRECISION':
  854. case 'DECIMAL':
  855. case 'DEC':
  856. case 'FIXED':
  857. default:
  858. //if (!is_numeric($t)) echo "<p>--- Error in type matching $t -----</p>";
  859. return 'N';
  860. }
  861. } // function
  862. } // rs class
  863. }
  864. class ADORecordSet_array_mysqli extends ADORecordSet_array {
  865. function ADORecordSet_array_mysqli($id=-1,$mode=false)
  866. {
  867. $this->ADORecordSet_array($id,$mode);
  868. }
  869. function MetaType($t, $len = -1, $fieldobj = false)
  870. {
  871. if (is_object($t)) {
  872. $fieldobj = $t;
  873. $t = $fieldobj->type;
  874. $len = $fieldobj->max_length;
  875. }
  876. $len = -1; // mysql max_length is not accurate
  877. switch (strtoupper($t)) {
  878. case 'STRING':
  879. case 'CHAR':
  880. case 'VARCHAR':
  881. case 'TINYBLOB':
  882. case 'TINYTEXT':
  883. case 'ENUM':
  884. case 'SET':
  885. case MYSQLI_TYPE_TINY_BLOB :
  886. case MYSQLI_TYPE_CHAR :
  887. case MYSQLI_TYPE_STRING :
  888. case MYSQLI_TYPE_ENUM :
  889. case MYSQLI_TYPE_SET :
  890. case 253 :
  891. if ($len <= $this->blobSize) return 'C';
  892. case 'TEXT':
  893. case 'LONGTEXT':
  894. case 'MEDIUMTEXT':
  895. return 'X';
  896. // php_mysql extension always returns 'blob' even if 'text'
  897. // so we have to check whether binary...
  898. case 'IMAGE':
  899. case 'LONGBLOB':
  900. case 'BLOB':
  901. case 'MEDIUMBLOB':
  902. case MYSQLI_TYPE_BLOB :
  903. case MYSQLI_TYPE_LONG_BLOB :
  904. case MYSQLI_TYPE_MEDIUM_BLOB :
  905. return !empty($fieldobj->binary) ? 'B' : 'X';
  906. case 'YEAR':
  907. case 'DATE':
  908. case MYSQLI_TYPE_DATE :
  909. case MYSQLI_TYPE_YEAR :
  910. return 'D';
  911. case 'TIME':
  912. case 'DATETIME':
  913. case 'TIMESTAMP':
  914. case MYSQLI_TYPE_DATETIME :
  915. case MYSQLI_TYPE_NEWDATE :
  916. case MYSQLI_TYPE_TIME :
  917. case MYSQLI_TYPE_TIMESTAMP :
  918. return 'T';
  919. case 'INT':
  920. case 'INTEGER':
  921. case 'BIGINT':
  922. case 'TINYINT':
  923. case 'MEDIUMINT':
  924. case 'SMALLINT':
  925. case MYSQLI_TYPE_INT24 :
  926. case MYSQLI_TYPE_LONG :
  927. case MYSQLI_TYPE_LONGLONG :
  928. case MYSQLI_TYPE_SHORT :
  929. case MYSQLI_TYPE_TINY :
  930. if (!empty($fieldobj->primary_key)) return 'R';
  931. return 'I';
  932. // Added floating-point types
  933. // Maybe not necessery.
  934. case 'FLOAT':
  935. case 'DOUBLE':
  936. // case 'DOUBLE PRECISION':
  937. case 'DECIMAL':
  938. case 'DEC':
  939. case 'FIXED':
  940. default:
  941. //if (!is_numeric($t)) echo "<p>--- Error in type matching $t -----</p>";
  942. return 'N';
  943. }
  944. } // function
  945. }
  946. ?>