PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/concreteOLD/libraries/3rdparty/adodb/drivers/adodb-mysqli.inc.php

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