See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]
1 <?php 2 /** 3 * MySQL improved driver (mysqli) 4 * 5 * This is the preferred driver for MySQL connections. It supports both 6 * transactional and non-transactional table types. You can use this as a 7 * drop-in replacement for both the mysql and mysqlt drivers. 8 * As of ADOdb Version 5.20.0, all other native MySQL drivers are deprecated. 9 * 10 * This file is part of ADOdb, a Database Abstraction Layer library for PHP. 11 * 12 * @package ADOdb 13 * @link https://adodb.org Project's web site and documentation 14 * @link https://github.com/ADOdb/ADOdb Source code and issue tracker 15 * 16 * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause 17 * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option, 18 * any later version. This means you can use it in proprietary products. 19 * See the LICENSE.md file distributed with this source code for details. 20 * @license BSD-3-Clause 21 * @license LGPL-2.1-or-later 22 * 23 * @copyright 2000-2013 John Lim 24 * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community 25 */ 26 27 // security - hide paths 28 if (!defined('ADODB_DIR')) { 29 die(); 30 } 31 32 if (!defined("_ADODB_MYSQLI_LAYER")) { 33 define("_ADODB_MYSQLI_LAYER", 1); 34 35 // PHP5 compat... 36 if (! defined("MYSQLI_BINARY_FLAG")) define("MYSQLI_BINARY_FLAG", 128); 37 if (!defined('MYSQLI_READ_DEFAULT_GROUP')) define('MYSQLI_READ_DEFAULT_GROUP',1); 38 39 /** 40 * Class ADODB_mysqli 41 */ 42 class ADODB_mysqli extends ADOConnection { 43 var $databaseType = 'mysqli'; 44 var $dataProvider = 'mysql'; 45 var $hasInsertID = true; 46 var $hasAffectedRows = true; 47 var $metaTablesSQL = "SELECT 48 TABLE_NAME, 49 CASE WHEN TABLE_TYPE = 'VIEW' THEN 'V' ELSE 'T' END 50 FROM INFORMATION_SCHEMA.TABLES 51 WHERE TABLE_SCHEMA="; 52 var $metaColumnsSQL = "SHOW COLUMNS FROM `%s`"; 53 var $fmtTimeStamp = "'Y-m-d H:i:s'"; 54 var $hasLimit = true; 55 var $hasMoveFirst = true; 56 var $hasGenID = true; 57 var $isoDates = true; // accepts dates in ISO format 58 var $sysDate = 'CURDATE()'; 59 var $sysTimeStamp = 'NOW()'; 60 var $hasTransactions = true; 61 var $forceNewConnect = false; 62 var $poorAffectedRows = true; 63 var $clientFlags = 0; 64 var $substr = "substring"; 65 var $port = 3306; //Default to 3306 to fix HHVM bug 66 var $socket = ''; //Default to empty string to fix HHVM bug 67 var $_bindInputArray = false; 68 var $nameQuote = '`'; /// string to use to quote identifiers and names 69 var $optionFlags = array(array(MYSQLI_READ_DEFAULT_GROUP,0)); 70 var $arrayClass = 'ADORecordSet_array_mysqli'; 71 var $multiQuery = false; 72 var $ssl_key = null; 73 var $ssl_cert = null; 74 var $ssl_ca = null; 75 var $ssl_capath = null; 76 var $ssl_cipher = null; 77 78 /** @var mysqli Identifier for the native database connection */ 79 var $_connectionID = false; 80 81 /** 82 * Tells the insert_id method how to obtain the last value, depending on whether 83 * we are using a stored procedure or not 84 */ 85 private $usePreparedStatement = false; 86 private $useLastInsertStatement = false; 87 private $usingBoundVariables = false; 88 private $statementAffectedRows = -1; 89 90 /** 91 * @var bool True if the last executed statement is a SELECT {@see _query()} 92 */ 93 private $isSelectStatement = false; 94 95 /** 96 * ADODB_mysqli constructor. 97 */ 98 public function __construct() 99 { 100 parent::__construct(); 101 102 // Forcing error reporting mode to OFF, which is no longer the default 103 // starting with PHP 8.1 (see #755) 104 mysqli_report(MYSQLI_REPORT_OFF); 105 } 106 107 108 /** 109 * Sets the isolation level of a transaction. 110 * 111 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:settransactionmode 112 * 113 * @param string $transaction_mode The transaction mode to set. 114 * 115 * @return void 116 */ 117 function SetTransactionMode($transaction_mode) 118 { 119 $this->_transmode = $transaction_mode; 120 if (empty($transaction_mode)) { 121 $this->execute('SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ'); 122 return; 123 } 124 if (!stristr($transaction_mode,'isolation')) $transaction_mode = 'ISOLATION LEVEL '.$transaction_mode; 125 $this->execute("SET SESSION TRANSACTION ".$transaction_mode); 126 } 127 128 /** 129 * Adds a parameter to the connection string. 130 * 131 * Parameter must be one of the constants listed in mysqli_options(). 132 * @see https://www.php.net/manual/en/mysqli.options.php 133 * 134 * @param int $parameter The parameter to set 135 * @param string $value The value of the parameter 136 * 137 * @return bool 138 */ 139 public function setConnectionParameter($parameter, $value) { 140 if(!is_numeric($parameter)) { 141 $this->outp_throw("Invalid connection parameter '$parameter'", __METHOD__); 142 return false; 143 } 144 return parent::setConnectionParameter($parameter, $value); 145 } 146 147 /** 148 * Connect to a database. 149 * 150 * @todo add: parameter int $port, parameter string $socket 151 * 152 * @param string|null $argHostname (Optional) The host to connect to. 153 * @param string|null $argUsername (Optional) The username to connect as. 154 * @param string|null $argPassword (Optional) The password to connect with. 155 * @param string|null $argDatabasename (Optional) The name of the database to start in when connected. 156 * @param bool $persist (Optional) Whether or not to use a persistent connection. 157 * 158 * @return bool|null True if connected successfully, false if connection failed, or null if the mysqli extension 159 * isn't currently loaded. 160 */ 161 function _connect($argHostname = null, 162 $argUsername = null, 163 $argPassword = null, 164 $argDatabasename = null, 165 $persist = false) 166 { 167 if(!extension_loaded("mysqli")) { 168 return null; 169 } 170 $this->_connectionID = @mysqli_init(); 171 172 if (is_null($this->_connectionID)) { 173 // mysqli_init only fails if insufficient memory 174 if ($this->debug) { 175 ADOConnection::outp("mysqli_init() failed : " . $this->errorMsg()); 176 } 177 return false; 178 } 179 /* 180 I suggest a simple fix which would enable adodb and mysqli driver to 181 read connection options from the standard mysql configuration file 182 /etc/my.cnf - "Bastien Duclaux" <bduclaux#yahoo.com> 183 */ 184 $this->optionFlags = array(); 185 foreach($this->optionFlags as $arr) { 186 mysqli_options($this->_connectionID,$arr[0],$arr[1]); 187 } 188 189 // Now merge in the standard connection parameters setting 190 foreach ($this->connectionParameters as $options) { 191 foreach ($options as $parameter => $value) { 192 // Make sure parameter is numeric before calling mysqli_options() 193 // to avoid Warning (or TypeError exception on PHP 8). 194 if (!is_numeric($parameter) 195 || !mysqli_options($this->_connectionID, $parameter, $value) 196 ) { 197 $this->outp_throw("Invalid connection parameter '$parameter'", __METHOD__); 198 } 199 } 200 } 201 202 //https://php.net/manual/en/mysqli.persistconns.php 203 if ($persist && strncmp($argHostname,'p:',2) != 0) { 204 $argHostname = 'p:' . $argHostname; 205 } 206 207 // SSL Connections for MySQLI 208 if ($this->ssl_key || $this->ssl_cert || $this->ssl_ca || $this->ssl_capath || $this->ssl_cipher) { 209 mysqli_ssl_set($this->_connectionID, $this->ssl_key, $this->ssl_cert, $this->ssl_ca, $this->ssl_capath, $this->ssl_cipher); 210 $this->socket = MYSQLI_CLIENT_SSL; 211 $this->clientFlags = MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT; 212 } 213 214 #if (!empty($this->port)) $argHostname .= ":".$this->port; 215 /** @noinspection PhpCastIsUnnecessaryInspection */ 216 $ok = @mysqli_real_connect($this->_connectionID, 217 $argHostname, 218 $argUsername, 219 $argPassword, 220 $argDatabasename, 221 # PHP7 compat: port must be int. Use default port if cast yields zero 222 (int)$this->port != 0 ? (int)$this->port : 3306, 223 $this->socket, 224 $this->clientFlags); 225 226 if ($ok) { 227 if ($argDatabasename) return $this->selectDB($argDatabasename); 228 return true; 229 } else { 230 if ($this->debug) { 231 ADOConnection::outp("Could not connect : " . $this->errorMsg()); 232 } 233 $this->_connectionID = null; 234 return false; 235 } 236 } 237 238 /** 239 * Connect to a database with a persistent connection. 240 * 241 * @param string|null $argHostname The host to connect to. 242 * @param string|null $argUsername The username to connect as. 243 * @param string|null $argPassword The password to connect with. 244 * @param string|null $argDatabasename The name of the database to start in when connected. 245 * 246 * @return bool|null True if connected successfully, false if connection failed, or null if the mysqli extension 247 * isn't currently loaded. 248 */ 249 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename) 250 { 251 return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, true); 252 } 253 254 /** 255 * Connect to a database, whilst setting $this->forceNewConnect to true. 256 * 257 * When is this used? Close old connection first? 258 * In _connect(), check $this->forceNewConnect? 259 * 260 * @param string|null $argHostname The host to connect to. 261 * @param string|null $argUsername The username to connect as. 262 * @param string|null $argPassword The password to connect with. 263 * @param string|null $argDatabaseName The name of the database to start in when connected. 264 * 265 * @return bool|null True if connected successfully, false if connection failed, or null if the mysqli extension 266 * isn't currently loaded. 267 */ 268 function _nconnect($argHostname, $argUsername, $argPassword, $argDatabaseName) 269 { 270 $this->forceNewConnect = true; 271 return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabaseName); 272 } 273 274 /** 275 * Replaces a null value with a specified replacement. 276 * 277 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:ifnull 278 * 279 * @param mixed $field The field in the table to check. 280 * @param mixed $ifNull The value to replace the null value with if it is found. 281 * 282 * @return string 283 */ 284 function IfNull($field, $ifNull) 285 { 286 return " IFNULL($field, $ifNull) "; 287 } 288 289 /** 290 * Retrieves the first column of the first matching row of an executed SQL statement. 291 * 292 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:getone 293 * 294 * @param string $sql The SQL to execute. 295 * @param bool|array $inputarr (Optional) An array containing any required SQL parameters, or false if none needed. 296 * 297 * @return bool|array|null 298 */ 299 function GetOne($sql, $inputarr = false) 300 { 301 global $ADODB_GETONE_EOF; 302 303 $ret = false; 304 $rs = $this->execute($sql,$inputarr); 305 if ($rs) { 306 if ($rs->EOF) $ret = $ADODB_GETONE_EOF; 307 else $ret = reset($rs->fields); 308 $rs->close(); 309 } 310 return $ret; 311 } 312 313 /** 314 * Get information about the current MySQL server. 315 * 316 * @return array 317 */ 318 function ServerInfo() 319 { 320 $arr['description'] = $this->getOne("select version()"); 321 $arr['version'] = ADOConnection::_findvers($arr['description']); 322 return $arr; 323 } 324 325 /** 326 * Begins a granular transaction. 327 * 328 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:begintrans 329 * 330 * @return bool Always returns true. 331 */ 332 function BeginTrans() 333 { 334 if ($this->transOff) return true; 335 $this->transCnt += 1; 336 337 //$this->execute('SET AUTOCOMMIT=0'); 338 mysqli_autocommit($this->_connectionID, false); 339 $this->execute('BEGIN'); 340 return true; 341 } 342 343 /** 344 * Commits a granular transaction. 345 * 346 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:committrans 347 * 348 * @param bool $ok (Optional) If false, will rollback the transaction instead. 349 * 350 * @return bool Always returns true. 351 */ 352 function CommitTrans($ok = true) 353 { 354 if ($this->transOff) return true; 355 if (!$ok) return $this->rollbackTrans(); 356 357 if ($this->transCnt) $this->transCnt -= 1; 358 $this->execute('COMMIT'); 359 360 //$this->execute('SET AUTOCOMMIT=1'); 361 mysqli_autocommit($this->_connectionID, true); 362 return true; 363 } 364 365 /** 366 * Rollback a smart transaction. 367 * 368 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:rollbacktrans 369 * 370 * @return bool Always returns true. 371 */ 372 function RollbackTrans() 373 { 374 if ($this->transOff) return true; 375 if ($this->transCnt) $this->transCnt -= 1; 376 $this->execute('ROLLBACK'); 377 //$this->execute('SET AUTOCOMMIT=1'); 378 mysqli_autocommit($this->_connectionID, true); 379 return true; 380 } 381 382 /** 383 * Lock a table row for a duration of a transaction. 384 * 385 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:rowlock 386 * 387 * @param string $table The table(s) to lock rows for. 388 * @param string $where (Optional) The WHERE clause to use to determine which rows to lock. 389 * @param string $col (Optional) The columns to select. 390 * 391 * @return bool True if the locking SQL statement executed successfully, otherwise false. 392 */ 393 function RowLock($table, $where = '', $col = '1 as adodbignore') 394 { 395 if ($this->transCnt==0) $this->beginTrans(); 396 if ($where) $where = ' where '.$where; 397 $rs = $this->execute("select $col from $table $where for update"); 398 return !empty($rs); 399 } 400 401 /** 402 * Appropriately quotes strings with ' characters for insertion into the database. 403 * 404 * Relies on mysqli_real_escape_string() 405 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:qstr 406 * 407 * @param string $s The string to quote 408 * @param bool $magic_quotes This param is not used since 5.21.0. 409 * It remains for backwards compatibility. 410 * 411 * @return string Quoted string 412 */ 413 function qStr($s, $magic_quotes=false) 414 { 415 if (is_null($s)) { 416 return 'NULL'; 417 } 418 419 // mysqli_real_escape_string() throws a warning when the given 420 // connection is invalid 421 if ($this->_connectionID) { 422 return "'" . mysqli_real_escape_string($this->_connectionID, $s) . "'"; 423 } 424 425 if ($this->replaceQuote[0] == '\\') { 426 $s = str_replace(array('\\', "\0"), array('\\\\', "\\\0") ,$s); 427 } 428 return "'" . str_replace("'", $this->replaceQuote, $s) . "'"; 429 } 430 431 /** 432 * Return the AUTO_INCREMENT id of the last row that has been inserted or updated in a table. 433 * 434 * @inheritDoc 435 */ 436 protected function _insertID($table = '', $column = '') 437 { 438 // mysqli_insert_id does not return the last_insert_id if called after 439 // execution of a stored procedure so we execute this instead. 440 if ($this->useLastInsertStatement) 441 $result = ADOConnection::getOne('SELECT LAST_INSERT_ID()'); 442 else 443 $result = @mysqli_insert_id($this->_connectionID); 444 445 if ($result == -1) { 446 if ($this->debug) 447 ADOConnection::outp("mysqli_insert_id() failed : " . $this->errorMsg()); 448 } 449 // reset prepared statement flags 450 $this->usePreparedStatement = false; 451 $this->useLastInsertStatement = false; 452 return $result; 453 } 454 455 /** 456 * Returns how many rows were effected by the most recently executed SQL statement. 457 * Only works for INSERT, UPDATE and DELETE queries. 458 * 459 * @return int The number of rows affected. 460 */ 461 function _affectedrows() 462 { 463 if ($this->isSelectStatement) { 464 // Affected rows works fine against selects, returning 465 // the rowcount, but ADOdb does not do that. 466 return false; 467 } 468 else if ($this->statementAffectedRows >= 0) 469 { 470 $result = $this->statementAffectedRows; 471 $this->statementAffectedRows = -1; 472 } 473 else 474 { 475 $result = @mysqli_affected_rows($this->_connectionID); 476 if ($result == -1) { 477 if ($this->debug) ADOConnection::outp("mysqli_affected_rows() failed : " . $this->errorMsg()); 478 } 479 } 480 return $result; 481 } 482 483 // Reference on Last_Insert_ID on the recommended way to simulate sequences 484 var $_genIDSQL = "update %s set id=LAST_INSERT_ID(id+1);"; 485 var $_genSeqSQL = "create table if not exists %s (id int not null)"; 486 var $_genSeqCountSQL = "select count(*) from %s"; 487 var $_genSeq2SQL = "insert into %s values (%s)"; 488 var $_dropSeqSQL = "drop table if exists %s"; 489 490 /** 491 * Creates a sequence in the database. 492 * 493 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:createsequence 494 * 495 * @param string $seqname The sequence name. 496 * @param int $startID The start id. 497 * 498 * @return ADORecordSet|bool A record set if executed successfully, otherwise false. 499 */ 500 function CreateSequence($seqname = 'adodbseq', $startID = 1) 501 { 502 if (empty($this->_genSeqSQL)) return false; 503 504 $ok = $this->execute(sprintf($this->_genSeqSQL,$seqname)); 505 if (!$ok) return false; 506 return $this->execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1)); 507 } 508 509 /** 510 * A portable method of creating sequence numbers. 511 * 512 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:genid 513 * 514 * @param string $seqname (Optional) The name of the sequence to use. 515 * @param int $startID (Optional) The point to start at in the sequence. 516 * 517 * @return bool|int|string 518 */ 519 function GenID($seqname = 'adodbseq', $startID = 1) 520 { 521 // post-nuke sets hasGenID to false 522 if (!$this->hasGenID) return false; 523 524 $getnext = sprintf($this->_genIDSQL,$seqname); 525 $holdtransOK = $this->_transOK; // save the current status 526 $rs = @$this->execute($getnext); 527 if (!$rs) { 528 if ($holdtransOK) $this->_transOK = true; //if the status was ok before reset 529 $this->execute(sprintf($this->_genSeqSQL,$seqname)); 530 $cnt = $this->getOne(sprintf($this->_genSeqCountSQL,$seqname)); 531 if (!$cnt) $this->execute(sprintf($this->_genSeq2SQL,$seqname,$startID-1)); 532 $rs = $this->execute($getnext); 533 } 534 535 if ($rs) { 536 $this->genID = mysqli_insert_id($this->_connectionID); 537 if ($this->genID == 0) { 538 $getnext = "select LAST_INSERT_ID() from " . $seqname; 539 $rs = $this->execute($getnext); 540 $this->genID = (int)$rs->fields[0]; 541 } 542 $rs->close(); 543 } else 544 $this->genID = 0; 545 546 return $this->genID; 547 } 548 549 /** 550 * Return a list of all visible databases except the 'mysql' database. 551 * 552 * @return array|false An array of database names, or false if the query failed. 553 */ 554 function MetaDatabases() 555 { 556 $query = "SHOW DATABASES"; 557 $ret = $this->execute($query); 558 if ($ret && is_object($ret)){ 559 $arr = array(); 560 while (!$ret->EOF){ 561 $db = $ret->fields('Database'); 562 if ($db != 'mysql') $arr[] = $db; 563 $ret->moveNext(); 564 } 565 return $arr; 566 } 567 return $ret; 568 } 569 570 /** 571 * Get a list of indexes on the specified table. 572 * 573 * @param string $table The name of the table to get indexes for. 574 * @param bool $primary (Optional) Whether or not to include the primary key. 575 * @param bool $owner (Optional) Unused. 576 * 577 * @return array|bool An array of the indexes, or false if the query to get the indexes failed. 578 */ 579 function MetaIndexes($table, $primary = false, $owner = false) 580 { 581 // save old fetch mode 582 global $ADODB_FETCH_MODE; 583 584 $save = $ADODB_FETCH_MODE; 585 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 586 if ($this->fetchMode !== FALSE) { 587 $savem = $this->setFetchMode(FALSE); 588 } 589 590 // get index details 591 $rs = $this->Execute(sprintf('SHOW INDEXES FROM `%s`',$table)); 592 593 // restore fetchmode 594 if (isset($savem)) { 595 $this->setFetchMode($savem); 596 } 597 $ADODB_FETCH_MODE = $save; 598 599 if (!is_object($rs)) { 600 return false; 601 } 602 603 $indexes = array (); 604 605 // parse index data into array 606 while ($row = $rs->fetchRow()) { 607 if ($primary == FALSE AND $row[2] == 'PRIMARY') { 608 continue; 609 } 610 611 if (!isset($indexes[$row[2]])) { 612 $indexes[$row[2]] = array( 613 'unique' => ($row[1] == 0), 614 'columns' => array() 615 ); 616 } 617 618 $indexes[$row[2]]['columns'][$row[3] - 1] = $row[4]; 619 } 620 621 // sort columns by order in the index 622 foreach ( array_keys ($indexes) as $index ) 623 { 624 ksort ($indexes[$index]['columns']); 625 } 626 627 return $indexes; 628 } 629 630 /** 631 * Returns a portably-formatted date string from a timestamp database column. 632 * 633 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:sqldate 634 * 635 * @param string $fmt The date format to use. 636 * @param string|bool $col (Optional) The table column to date format, or if false, use NOW(). 637 * 638 * @return string The SQL DATE_FORMAT() string, or false if the provided date format was empty. 639 */ 640 function SQLDate($fmt, $col = false) 641 { 642 if (!$col) $col = $this->sysTimeStamp; 643 $s = 'DATE_FORMAT('.$col.",'"; 644 $concat = false; 645 $len = strlen($fmt); 646 for ($i=0; $i < $len; $i++) { 647 $ch = $fmt[$i]; 648 switch($ch) { 649 case 'Y': 650 case 'y': 651 $s .= '%Y'; 652 break; 653 case 'Q': 654 case 'q': 655 $s .= "'),Quarter($col)"; 656 657 if ($len > $i+1) $s .= ",DATE_FORMAT($col,'"; 658 else $s .= ",('"; 659 $concat = true; 660 break; 661 case 'M': 662 $s .= '%b'; 663 break; 664 665 case 'm': 666 $s .= '%m'; 667 break; 668 case 'D': 669 case 'd': 670 $s .= '%d'; 671 break; 672 673 case 'H': 674 $s .= '%H'; 675 break; 676 677 case 'h': 678 $s .= '%I'; 679 break; 680 681 case 'i': 682 $s .= '%i'; 683 break; 684 685 case 's': 686 $s .= '%s'; 687 break; 688 689 case 'a': 690 case 'A': 691 $s .= '%p'; 692 break; 693 694 case 'w': 695 $s .= '%w'; 696 break; 697 698 case 'l': 699 $s .= '%W'; 700 break; 701 702 default: 703 704 if ($ch == '\\') { 705 $i++; 706 $ch = substr($fmt,$i,1); 707 } 708 $s .= $ch; 709 break; 710 } 711 } 712 $s.="')"; 713 if ($concat) $s = "CONCAT($s)"; 714 return $s; 715 } 716 717 /** 718 * Returns a database-specific concatenation of strings. 719 * 720 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:concat 721 * 722 * @return string 723 */ 724 function Concat() 725 { 726 $arr = func_get_args(); 727 728 // suggestion by andrew005@mnogo.ru 729 $s = implode(',',$arr); 730 if (strlen($s) > 0) return "CONCAT($s)"; 731 else return ''; 732 } 733 734 /** 735 * Creates a portable date offset field, for use in SQL statements. 736 * 737 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:offsetdate 738 * 739 * @param float $dayFraction A day in floating point 740 * @param string|bool $date (Optional) The date to offset. If false, uses CURDATE() 741 * 742 * @return string 743 */ 744 function OffsetDate($dayFraction, $date = false) 745 { 746 if (!$date) $date = $this->sysDate; 747 748 $fraction = $dayFraction * 24 * 3600; 749 return $date . ' + INTERVAL ' . $fraction.' SECOND'; 750 751 // return "from_unixtime(unix_timestamp($date)+$fraction)"; 752 } 753 754 /** 755 * Returns information about stored procedures and stored functions. 756 * 757 * @param string|bool $procedureNamePattern (Optional) Only look for procedures/functions with a name matching this pattern. 758 * @param null $catalog (Optional) Unused. 759 * @param null $schemaPattern (Optional) Unused. 760 * 761 * @return array 762 */ 763 function MetaProcedures($procedureNamePattern = false, $catalog = null, $schemaPattern = null) 764 { 765 // save old fetch mode 766 global $ADODB_FETCH_MODE; 767 768 $save = $ADODB_FETCH_MODE; 769 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 770 771 if ($this->fetchMode !== FALSE) { 772 $savem = $this->setFetchMode(FALSE); 773 } 774 775 $procedures = array (); 776 777 // get index details 778 779 $likepattern = ''; 780 if ($procedureNamePattern) { 781 $likepattern = " LIKE '".$procedureNamePattern."'"; 782 } 783 $rs = $this->execute('SHOW PROCEDURE STATUS'.$likepattern); 784 if (is_object($rs)) { 785 786 // parse index data into array 787 while ($row = $rs->fetchRow()) { 788 $procedures[$row[1]] = array( 789 'type' => 'PROCEDURE', 790 'catalog' => '', 791 'schema' => '', 792 'remarks' => $row[7], 793 ); 794 } 795 } 796 797 $rs = $this->execute('SHOW FUNCTION STATUS'.$likepattern); 798 if (is_object($rs)) { 799 // parse index data into array 800 while ($row = $rs->fetchRow()) { 801 $procedures[$row[1]] = array( 802 'type' => 'FUNCTION', 803 'catalog' => '', 804 'schema' => '', 805 'remarks' => $row[7] 806 ); 807 } 808 } 809 810 // restore fetchmode 811 if (isset($savem)) { 812 $this->setFetchMode($savem); 813 } 814 $ADODB_FETCH_MODE = $save; 815 816 return $procedures; 817 } 818 819 /** 820 * Retrieves a list of tables based on given criteria 821 * 822 * @param string|bool $ttype (Optional) Table type = 'TABLE', 'VIEW' or false=both (default) 823 * @param string|bool $showSchema (Optional) schema name, false = current schema (default) 824 * @param string|bool $mask (Optional) filters the table by name 825 * 826 * @return array list of tables 827 */ 828 function MetaTables($ttype = false, $showSchema = false, $mask = false) 829 { 830 $save = $this->metaTablesSQL; 831 if ($showSchema && is_string($showSchema)) { 832 $this->metaTablesSQL .= $this->qstr($showSchema); 833 } else { 834 $this->metaTablesSQL .= "schema()"; 835 } 836 837 if ($mask) { 838 $mask = $this->qstr($mask); 839 $this->metaTablesSQL .= " AND table_name LIKE $mask"; 840 } 841 $ret = ADOConnection::metaTables($ttype,$showSchema); 842 843 $this->metaTablesSQL = $save; 844 return $ret; 845 } 846 847 /** 848 * Return information about a table's foreign keys. 849 * 850 * @param string $table The name of the table to get the foreign keys for. 851 * @param string|bool $owner (Optional) The database the table belongs to, or false to assume the current db. 852 * @param string|bool $upper (Optional) Force uppercase table name on returned array keys. 853 * @param bool $associative (Optional) Whether to return an associate or numeric array. 854 * 855 * @return array|bool An array of foreign keys, or false no foreign keys could be found. 856 */ 857 public function metaForeignKeys($table, $owner = '', $upper = false, $associative = false) 858 { 859 global $ADODB_FETCH_MODE; 860 861 if ($ADODB_FETCH_MODE == ADODB_FETCH_ASSOC 862 || $this->fetchMode == ADODB_FETCH_ASSOC) 863 $associative = true; 864 865 $savem = $ADODB_FETCH_MODE; 866 $this->setFetchMode(ADODB_FETCH_ASSOC); 867 868 if ( !empty($owner) ) { 869 $table = "$owner.$table"; 870 } 871 872 $a_create_table = $this->getRow(sprintf('SHOW CREATE TABLE `%s`', $table)); 873 874 $this->setFetchMode($savem); 875 876 $create_sql = isset($a_create_table["Create Table"]) ? $a_create_table["Create Table"] : $a_create_table["Create View"]; 877 878 $matches = array(); 879 880 if (!preg_match_all("/FOREIGN KEY \(`(.*?)`\) REFERENCES `(.*?)` \(`(.*?)`\)/", $create_sql, $matches)) return false; 881 $foreign_keys = array(); 882 $num_keys = count($matches[0]); 883 for ( $i = 0; $i < $num_keys; $i ++ ) { 884 $my_field = explode('`, `', $matches[1][$i]); 885 $ref_table = $matches[2][$i]; 886 $ref_field = explode('`, `', $matches[3][$i]); 887 888 if ( $upper ) { 889 $ref_table = strtoupper($ref_table); 890 } 891 892 // see https://sourceforge.net/p/adodb/bugs/100/ 893 if (!isset($foreign_keys[$ref_table])) { 894 $foreign_keys[$ref_table] = array(); 895 } 896 $num_fields = count($my_field); 897 for ( $j = 0; $j < $num_fields; $j ++ ) { 898 if ( $associative ) { 899 $foreign_keys[$ref_table][$ref_field[$j]] = $my_field[$j]; 900 } else { 901 $foreign_keys[$ref_table][] = "{$my_field[$j]}={$ref_field[$j]}"; 902 } 903 } 904 } 905 906 return $foreign_keys; 907 } 908 909 /** 910 * Return an array of information about a table's columns. 911 * 912 * @param string $table The name of the table to get the column info for. 913 * @param bool $normalize (Optional) Unused. 914 * 915 * @return ADOFieldObject[]|bool An array of info for each column, or false if it could not determine the info. 916 */ 917 function MetaColumns($table, $normalize = true) 918 { 919 $false = false; 920 if (!$this->metaColumnsSQL) 921 return $false; 922 923 global $ADODB_FETCH_MODE; 924 $save = $ADODB_FETCH_MODE; 925 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 926 if ($this->fetchMode !== false) 927 $savem = $this->SetFetchMode(false); 928 /* 929 * Return assoc array where key is column name, value is column type 930 * [1] => int unsigned 931 */ 932 933 $SQL = "SELECT column_name, column_type 934 FROM information_schema.columns 935 WHERE table_schema='{$this->databaseName}' 936 AND table_name='$table'"; 937 938 $schemaArray = $this->getAssoc($SQL); 939 $schemaArray = array_change_key_case($schemaArray,CASE_LOWER); 940 941 $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table)); 942 if (isset($savem)) $this->SetFetchMode($savem); 943 $ADODB_FETCH_MODE = $save; 944 if (!is_object($rs)) 945 return $false; 946 947 $retarr = array(); 948 while (!$rs->EOF) { 949 $fld = new ADOFieldObject(); 950 $fld->name = $rs->fields[0]; 951 952 /* 953 * Type from information_schema returns 954 * the same format in V8 mysql as V5 955 */ 956 $type = $schemaArray[strtolower($fld->name)]; 957 958 // split type into type(length): 959 $fld->scale = null; 960 if (preg_match("/^(.+)\((\d+),(\d+)/", $type, $query_array)) { 961 $fld->type = $query_array[1]; 962 $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1; 963 $fld->scale = is_numeric($query_array[3]) ? $query_array[3] : -1; 964 } elseif (preg_match("/^(.+)\((\d+)/", $type, $query_array)) { 965 $fld->type = $query_array[1]; 966 $fld->max_length = is_numeric($query_array[2]) ? $query_array[2] : -1; 967 } elseif (preg_match("/^(enum)\((.*)\)$/i", $type, $query_array)) { 968 $fld->type = $query_array[1]; 969 $arr = explode(",",$query_array[2]); 970 $fld->enums = $arr; 971 $zlen = max(array_map("strlen",$arr)) - 2; // PHP >= 4.0.6 972 $fld->max_length = ($zlen > 0) ? $zlen : 1; 973 } else { 974 $fld->type = $type; 975 $fld->max_length = -1; 976 } 977 978 $fld->not_null = ($rs->fields[2] != 'YES'); 979 $fld->primary_key = ($rs->fields[3] == 'PRI'); 980 $fld->auto_increment = (strpos($rs->fields[5], 'auto_increment') !== false); 981 $fld->binary = (strpos($type,'blob') !== false); 982 $fld->unsigned = (strpos($type,'unsigned') !== false); 983 $fld->zerofill = (strpos($type,'zerofill') !== false); 984 985 if (!$fld->binary) { 986 $d = $rs->fields[4]; 987 if ($d != '' && $d != 'NULL') { 988 $fld->has_default = true; 989 $fld->default_value = $d; 990 } else { 991 $fld->has_default = false; 992 } 993 } 994 995 if ($save == ADODB_FETCH_NUM) { 996 $retarr[] = $fld; 997 } else { 998 $retarr[strtoupper($fld->name)] = $fld; 999 } 1000 $rs->moveNext(); 1001 } 1002 1003 $rs->close(); 1004 return $retarr; 1005 } 1006 1007 /** 1008 * Select which database to connect to. 1009 * 1010 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:selectdb 1011 * 1012 * @param string $dbName The name of the database to select. 1013 * 1014 * @return bool True if the database was selected successfully, otherwise false. 1015 */ 1016 function SelectDB($dbName) 1017 { 1018 // $this->_connectionID = $this->mysqli_resolve_link($this->_connectionID); 1019 $this->database = $dbName; 1020 $this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions 1021 1022 if ($this->_connectionID) { 1023 $result = @mysqli_select_db($this->_connectionID, $dbName); 1024 if (!$result) { 1025 ADOConnection::outp("Select of database " . $dbName . " failed. " . $this->errorMsg()); 1026 } 1027 return $result; 1028 } 1029 return false; 1030 } 1031 1032 /** 1033 * Executes a provided SQL statement and returns a handle to the result, with the ability to supply a starting 1034 * offset and record count. 1035 * 1036 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:selectlimit 1037 * 1038 * @param string $sql The SQL to execute. 1039 * @param int $nrows (Optional) The limit for the number of records you want returned. By default, all results. 1040 * @param int $offset (Optional) The offset to use when selecting the results. By default, no offset. 1041 * @param array|bool $inputarr (Optional) Any parameter values required by the SQL statement, or false if none. 1042 * @param int $secs2cache (Optional) If greater than 0, perform a cached execute. By default, normal execution. 1043 * 1044 * @return ADORecordSet|false The query results, or false if the query failed to execute. 1045 */ 1046 function SelectLimit($sql, 1047 $nrows = -1, 1048 $offset = -1, 1049 $inputarr = false, 1050 $secs2cache = 0) 1051 { 1052 $nrows = (int) $nrows; 1053 $offset = (int) $offset; 1054 $offsetStr = ($offset >= 0) ? "$offset," : ''; 1055 if ($nrows < 0) $nrows = '18446744073709551615'; 1056 1057 if ($secs2cache) 1058 $rs = $this->cacheExecute($secs2cache, $sql . " LIMIT $offsetStr$nrows" , $inputarr ); 1059 else 1060 $rs = $this->execute($sql . " LIMIT $offsetStr$nrows" , $inputarr ); 1061 1062 return $rs; 1063 } 1064 1065 /** 1066 * Prepares an SQL statement and returns a handle to use. 1067 * This is not used by bound parameters anymore 1068 * 1069 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:prepare 1070 * @todo update this function to handle prepared statements correctly 1071 * 1072 * @param string $sql The SQL to prepare. 1073 * 1074 * @return string The original SQL that was provided. 1075 */ 1076 function Prepare($sql) 1077 { 1078 /* 1079 * Flag the insert_id method to use the correct retrieval method 1080 */ 1081 $this->usePreparedStatement = true; 1082 1083 /* 1084 * Prepared statements are not yet handled correctly 1085 */ 1086 return $sql; 1087 $stmt = $this->_connectionID->prepare($sql); 1088 if (!$stmt) { 1089 echo $this->errorMsg(); 1090 return $sql; 1091 } 1092 return array($sql,$stmt); 1093 } 1094 1095 /** 1096 * Execute SQL 1097 * 1098 * @param string $sql SQL statement to execute, or possibly an array 1099 * holding prepared statement ($sql[0] will hold sql text) 1100 * @param array|bool $inputarr holds the input data to bind to. 1101 * Null elements will be set to null. 1102 * 1103 * @return ADORecordSet|bool 1104 */ 1105 public function execute($sql, $inputarr = false) 1106 { 1107 if ($this->fnExecute) { 1108 $fn = $this->fnExecute; 1109 $ret = $fn($this, $sql, $inputarr); 1110 if (isset($ret)) { 1111 return $ret; 1112 } 1113 } 1114 1115 if ($inputarr === false || $inputarr === []) { 1116 return $this->_execute($sql); 1117 } 1118 1119 if (!is_array($inputarr)) { 1120 $inputarr = array($inputarr); 1121 } 1122 1123 if (!is_array($sql)) { 1124 // Check if we are bulkbinding. If so, $inputarr is a 2d array, 1125 // and we make a gross assumption that all rows have the same number 1126 // of columns of the same type, and use the elements of the first row 1127 // to determine the MySQL bind param types. 1128 if (is_array($inputarr[0])) { 1129 if (!$this->bulkBind) { 1130 $this->outp_throw( 1131 "2D Array of values sent to execute and 'ADOdb_mysqli::bulkBind' not set", 1132 'Execute' 1133 ); 1134 return false; 1135 } 1136 1137 $bulkTypeArray = []; 1138 foreach ($inputarr as $v) { 1139 if (is_string($this->bulkBind)) { 1140 $typeArray = array_merge((array)$this->bulkBind, $v); 1141 } else { 1142 $typeArray = $this->getBindParamWithType($v); 1143 } 1144 $bulkTypeArray[] = $typeArray; 1145 } 1146 $this->bulkBind = false; 1147 $ret = $this->_execute($sql, $bulkTypeArray); 1148 } else { 1149 $typeArray = $this->getBindParamWithType($inputarr); 1150 $ret = $this->_execute($sql, $typeArray); 1151 } 1152 } else { 1153 $ret = $this->_execute($sql, $inputarr); 1154 } 1155 return $ret; 1156 } 1157 1158 /** 1159 * Inserts the bind param type string at the front of the parameter array. 1160 * 1161 * @see https://www.php.net/manual/en/mysqli-stmt.bind-param.php 1162 * 1163 * @param array $inputArr 1164 * @return array 1165 */ 1166 private function getBindParamWithType($inputArr): array 1167 { 1168 $typeString = ''; 1169 foreach ($inputArr as $v) { 1170 if (is_integer($v) || is_bool($v)) { 1171 $typeString .= 'i'; 1172 } elseif (is_float($v)) { 1173 $typeString .= 'd'; 1174 } elseif (is_object($v)) { 1175 // Assume a blob 1176 $typeString .= 'b'; 1177 } else { 1178 $typeString .= 's'; 1179 } 1180 } 1181 1182 // Place the field type list at the front of the parameter array. 1183 // This is the mysql specific format 1184 array_unshift($inputArr, $typeString); 1185 return $inputArr; 1186 } 1187 1188 /** 1189 * Return the query id. 1190 * 1191 * @param string|array $sql 1192 * @param array $inputarr 1193 * 1194 * @return bool|mysqli_result 1195 */ 1196 function _query($sql, $inputarr) 1197 { 1198 global $ADODB_COUNTRECS; 1199 // Move to the next recordset, or return false if there is none. In a stored proc 1200 // call, mysqli_next_result returns true for the last "recordset", but mysqli_store_result 1201 // returns false. I think this is because the last "recordset" is actually just the 1202 // return value of the stored proc (ie the number of rows affected). 1203 // Commented out for reasons of performance. You should retrieve every recordset yourself. 1204 // if (!mysqli_next_result($this->connection->_connectionID)) return false; 1205 1206 if (is_array($sql)) { 1207 1208 // Prepare() not supported because mysqli_stmt_execute does not return a recordset, but 1209 // returns as bound variables. 1210 1211 $stmt = $sql[1]; 1212 $a = ''; 1213 foreach($inputarr as $v) { 1214 if (is_string($v)) $a .= 's'; 1215 else if (is_integer($v)) $a .= 'i'; 1216 else $a .= 'd'; 1217 } 1218 1219 /* 1220 * set prepared statement flags 1221 */ 1222 if ($this->usePreparedStatement) 1223 $this->useLastInsertStatement = true; 1224 1225 $fnarr = array_merge( array($stmt,$a) , $inputarr); 1226 call_user_func_array('mysqli_stmt_bind_param',$fnarr); 1227 return mysqli_stmt_execute($stmt); 1228 } 1229 else if (is_string($sql) && is_array($inputarr)) 1230 { 1231 1232 /* 1233 * This is support for true prepared queries 1234 * with bound parameters 1235 * 1236 * set prepared statement flags 1237 */ 1238 $this->usePreparedStatement = true; 1239 $this->usingBoundVariables = true; 1240 1241 $bulkBindArray = array(); 1242 if (is_array($inputarr[0])) 1243 { 1244 $bulkBindArray = $inputarr; 1245 $inputArrayCount = count($inputarr[0]) - 1; 1246 } 1247 else 1248 { 1249 $bulkBindArray[] = $inputarr; 1250 $inputArrayCount = count($inputarr) - 1; 1251 } 1252 1253 1254 /* 1255 * Prepare the statement with the placeholders, 1256 * prepare will fail if the statement is invalid 1257 * so we trap and error if necessary. Note that we 1258 * are calling MySQL prepare here, not ADOdb 1259 */ 1260 $stmt = $this->_connectionID->prepare($sql); 1261 if ($stmt === false) 1262 { 1263 $this->outp_throw( 1264 "SQL Statement failed on preparation: " . htmlspecialchars($sql) . "'", 1265 'Execute' 1266 ); 1267 return false; 1268 } 1269 /* 1270 * Make sure the number of parameters provided in the input 1271 * array matches what the query expects. We must discount 1272 * the first parameter which contains the data types in 1273 * our inbound parameters 1274 */ 1275 $nparams = $stmt->param_count; 1276 1277 if ($nparams != $inputArrayCount) 1278 { 1279 1280 $this->outp_throw( 1281 "Input array has " . $inputArrayCount . 1282 " params, does not match query: '" . htmlspecialchars($sql) . "'", 1283 'Execute' 1284 ); 1285 return false; 1286 } 1287 1288 foreach ($bulkBindArray as $inputarr) 1289 { 1290 /* 1291 * Must pass references into call_user_func_array 1292 */ 1293 $paramsByReference = array(); 1294 foreach($inputarr as $key => $value) { 1295 /** @noinspection PhpArrayAccessCanBeReplacedWithForeachValueInspection */ 1296 $paramsByReference[$key] = &$inputarr[$key]; 1297 } 1298 1299 /* 1300 * Bind the params 1301 */ 1302 call_user_func_array(array($stmt, 'bind_param'), $paramsByReference); 1303 1304 /* 1305 * Execute 1306 */ 1307 1308 $ret = mysqli_stmt_execute($stmt); 1309 1310 /* 1311 * Did we throw an error? 1312 */ 1313 if ($ret == false) 1314 return false; 1315 } 1316 1317 // Tells affected_rows to be compliant 1318 $this->isSelectStatement = $stmt->affected_rows == -1; 1319 if (!$this->isSelectStatement) { 1320 $this->statementAffectedRows = $stmt->affected_rows; 1321 return true; 1322 } 1323 1324 // Turn the statement into a result set and return it 1325 return $stmt->get_result(); 1326 } 1327 else 1328 { 1329 /* 1330 * reset prepared statement flags, in case we set them 1331 * previously and didn't use them 1332 */ 1333 $this->usePreparedStatement = false; 1334 $this->useLastInsertStatement = false; 1335 } 1336 1337 /* 1338 if (!$mysql_res = mysqli_query($this->_connectionID, $sql, ($ADODB_COUNTRECS) ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT)) { 1339 if ($this->debug) ADOConnection::outp("Query: " . $sql . " failed. " . $this->errorMsg()); 1340 return false; 1341 } 1342 1343 return $mysql_res; 1344 */ 1345 1346 if ($this->multiQuery) { 1347 $rs = mysqli_multi_query($this->_connectionID, $sql.';'); 1348 if ($rs) { 1349 $rs = ($ADODB_COUNTRECS) ? @mysqli_store_result( $this->_connectionID ) : @mysqli_use_result( $this->_connectionID ); 1350 return $rs ?: true; // mysqli_more_results( $this->_connectionID ) 1351 } 1352 } else { 1353 $rs = mysqli_query($this->_connectionID, $sql, $ADODB_COUNTRECS ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); 1354 if ($rs) { 1355 $this->isSelectStatement = is_object($rs); 1356 return $rs; 1357 } 1358 } 1359 1360 if($this->debug) 1361 ADOConnection::outp("Query: " . $sql . " failed. " . $this->errorMsg()); 1362 1363 return false; 1364 1365 } 1366 1367 /** 1368 * Returns a database specific error message. 1369 * 1370 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:errormsg 1371 * 1372 * @return string The last error message. 1373 */ 1374 function ErrorMsg() 1375 { 1376 if (empty($this->_connectionID)) 1377 $this->_errorMsg = @mysqli_connect_error(); 1378 else 1379 $this->_errorMsg = @mysqli_error($this->_connectionID); 1380 return $this->_errorMsg; 1381 } 1382 1383 /** 1384 * Returns the last error number from previous database operation. 1385 * 1386 * @return int The last error number. 1387 */ 1388 function ErrorNo() 1389 { 1390 if (empty($this->_connectionID)) 1391 return @mysqli_connect_errno(); 1392 else 1393 return @mysqli_errno($this->_connectionID); 1394 } 1395 1396 /** 1397 * Close the database connection. 1398 * 1399 * @return void 1400 */ 1401 function _close() 1402 { 1403 if($this->_connectionID) { 1404 mysqli_close($this->_connectionID); 1405 } 1406 $this->_connectionID = false; 1407 } 1408 1409 /** 1410 * Returns the largest length of data that can be inserted into a character field. 1411 * 1412 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:charmax 1413 * 1414 * @return int 1415 */ 1416 function CharMax() 1417 { 1418 return 255; 1419 } 1420 1421 /** 1422 * Returns the largest length of data that can be inserted into a text field. 1423 * 1424 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:textmax 1425 * 1426 * @return int 1427 */ 1428 function TextMax() 1429 { 1430 return 4294967295; 1431 } 1432 1433 function getCharSet() 1434 { 1435 if (!$this->_connectionID || !method_exists($this->_connectionID,'character_set_name')) { 1436 return false; 1437 } 1438 1439 $this->charSet = $this->_connectionID->character_set_name(); 1440 return $this->charSet ?: false; 1441 } 1442 1443 function setCharSet($charset) 1444 { 1445 if (!$this->_connectionID || !method_exists($this->_connectionID,'set_charset')) { 1446 return false; 1447 } 1448 1449 if ($this->charSet !== $charset) { 1450 if (!$this->_connectionID->set_charset($charset)) { 1451 return false; 1452 } 1453 $this->getCharSet(); 1454 } 1455 return true; 1456 } 1457 1458 } 1459 1460 /** 1461 * Class ADORecordSet_mysqli 1462 */ 1463 class ADORecordSet_mysqli extends ADORecordSet{ 1464 1465 var $databaseType = "mysqli"; 1466 var $canSeek = true; 1467 1468 /** @var ADODB_mysqli The parent connection */ 1469 var $connection = false; 1470 1471 /** @var mysqli_result result link identifier */ 1472 var $_queryID; 1473 1474 function __construct($queryID, $mode = false) 1475 { 1476 if ($mode === false) { 1477 global $ADODB_FETCH_MODE; 1478 $mode = $ADODB_FETCH_MODE; 1479 } 1480 1481 switch ($mode) { 1482 case ADODB_FETCH_NUM: 1483 $this->fetchMode = MYSQLI_NUM; 1484 break; 1485 case ADODB_FETCH_ASSOC: 1486 $this->fetchMode = MYSQLI_ASSOC; 1487 break; 1488 case ADODB_FETCH_DEFAULT: 1489 case ADODB_FETCH_BOTH: 1490 default: 1491 $this->fetchMode = MYSQLI_BOTH; 1492 break; 1493 } 1494 $this->adodbFetchMode = $mode; 1495 parent::__construct($queryID); 1496 } 1497 1498 function _initrs() 1499 { 1500 global $ADODB_COUNTRECS; 1501 1502 $this->_numOfRows = $ADODB_COUNTRECS ? @mysqli_num_rows($this->_queryID) : -1; 1503 $this->_numOfFields = @mysqli_num_fields($this->_queryID); 1504 } 1505 1506 /* 1507 1 = MYSQLI_NOT_NULL_FLAG 1508 2 = MYSQLI_PRI_KEY_FLAG 1509 4 = MYSQLI_UNIQUE_KEY_FLAG 1510 8 = MYSQLI_MULTIPLE_KEY_FLAG 1511 16 = MYSQLI_BLOB_FLAG 1512 32 = MYSQLI_UNSIGNED_FLAG 1513 64 = MYSQLI_ZEROFILL_FLAG 1514 128 = MYSQLI_BINARY_FLAG 1515 256 = MYSQLI_ENUM_FLAG 1516 512 = MYSQLI_AUTO_INCREMENT_FLAG 1517 1024 = MYSQLI_TIMESTAMP_FLAG 1518 2048 = MYSQLI_SET_FLAG 1519 32768 = MYSQLI_NUM_FLAG 1520 16384 = MYSQLI_PART_KEY_FLAG 1521 32768 = MYSQLI_GROUP_FLAG 1522 65536 = MYSQLI_UNIQUE_FLAG 1523 131072 = MYSQLI_BINCMP_FLAG 1524 */ 1525 1526 /** 1527 * Returns raw, database specific information about a field. 1528 * 1529 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:recordset:fetchfield 1530 * 1531 * @param int $fieldOffset (Optional) The field number to get information for. 1532 * 1533 * @return ADOFieldObject|bool 1534 */ 1535 function FetchField($fieldOffset = -1) 1536 { 1537 $fieldnr = $fieldOffset; 1538 if ($fieldOffset != -1) { 1539 $fieldOffset = @mysqli_field_seek($this->_queryID, $fieldnr); 1540 } 1541 $o = @mysqli_fetch_field($this->_queryID); 1542 if (!$o) return false; 1543 1544 //Fix for HHVM 1545 if ( !isset($o->flags) ) { 1546 $o->flags = 0; 1547 } 1548 /* Properties of an ADOFieldObject as set by MetaColumns */ 1549 $o->primary_key = $o->flags & MYSQLI_PRI_KEY_FLAG; 1550 $o->not_null = $o->flags & MYSQLI_NOT_NULL_FLAG; 1551 $o->auto_increment = $o->flags & MYSQLI_AUTO_INCREMENT_FLAG; 1552 $o->binary = $o->flags & MYSQLI_BINARY_FLAG; 1553 // $o->blob = $o->flags & MYSQLI_BLOB_FLAG; /* not returned by MetaColumns */ 1554 $o->unsigned = $o->flags & MYSQLI_UNSIGNED_FLAG; 1555 1556 /* 1557 * Trivial method to cast class to ADOfieldObject 1558 */ 1559 $a = new ADOFieldObject; 1560 foreach (get_object_vars($o) as $key => $name) 1561 $a->$key = $name; 1562 return $a; 1563 } 1564 1565 /** 1566 * Reads a row in associative mode if the recordset fetch mode is numeric. 1567 * Using this function when the fetch mode is set to ADODB_FETCH_ASSOC may produce unpredictable results. 1568 * 1569 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:getrowassoc 1570 * 1571 * @param int $upper Indicates whether the keys of the recordset should be upper case or lower case. 1572 * 1573 * @return array|bool 1574 */ 1575 function GetRowAssoc($upper = ADODB_ASSOC_CASE) 1576 { 1577 if ($this->fetchMode == MYSQLI_ASSOC && $upper == ADODB_ASSOC_CASE_LOWER) { 1578 return $this->fields; 1579 } 1580 return ADORecordSet::getRowAssoc($upper); 1581 } 1582 1583 /** 1584 * Returns a single field in a single row of the current recordset. 1585 * 1586 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:recordset:fields 1587 * 1588 * @param string $colname The name of the field to retrieve. 1589 * 1590 * @return mixed 1591 */ 1592 function Fields($colname) 1593 { 1594 if ($this->fetchMode != MYSQLI_NUM) { 1595 return @$this->fields[$colname]; 1596 } 1597 1598 if (!$this->bind) { 1599 $this->bind = array(); 1600 for ($i = 0; $i < $this->_numOfFields; $i++) { 1601 $o = $this->fetchField($i); 1602 $this->bind[strtoupper($o->name)] = $i; 1603 } 1604 } 1605 return $this->fields[$this->bind[strtoupper($colname)]]; 1606 } 1607 1608 /** 1609 * Adjusts the result pointer to an arbitrary row in the result. 1610 * 1611 * @param int $row The row to seek to. 1612 * 1613 * @return bool False if the recordset contains no rows, otherwise true. 1614 */ 1615 function _seek($row) 1616 { 1617 if ($this->_numOfRows == 0 || $row < 0) { 1618 return false; 1619 } 1620 1621 mysqli_data_seek($this->_queryID, $row); 1622 $this->EOF = false; 1623 return true; 1624 } 1625 1626 /** 1627 * In databases that allow accessing of recordsets, retrieves the next set. 1628 * 1629 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:recordset:nextrecordset 1630 * 1631 * @return bool 1632 */ 1633 function NextRecordSet() 1634 { 1635 global $ADODB_COUNTRECS; 1636 1637 mysqli_free_result($this->_queryID); 1638 $this->_queryID = -1; 1639 // Move to the next recordset, or return false if there is none. In a stored proc 1640 // call, mysqli_next_result returns true for the last "recordset", but mysqli_store_result 1641 // returns false. I think this is because the last "recordset" is actually just the 1642 // return value of the stored proc (ie the number of rows affected). 1643 if (!mysqli_next_result($this->connection->_connectionID)) { 1644 return false; 1645 } 1646 1647 // CD: There is no $this->_connectionID variable, at least in the ADO version I'm using 1648 $this->_queryID = ($ADODB_COUNTRECS) ? @mysqli_store_result($this->connection->_connectionID) 1649 : @mysqli_use_result($this->connection->_connectionID); 1650 1651 if (!$this->_queryID) { 1652 return false; 1653 } 1654 1655 $this->_inited = false; 1656 $this->bind = false; 1657 $this->_currentRow = -1; 1658 $this->init(); 1659 return true; 1660 } 1661 1662 /** 1663 * Moves the cursor to the next record of the recordset from the current position. 1664 * 1665 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:movenext 1666 * 1667 * @return bool False if there are no more records to move on to, otherwise true. 1668 */ 1669 function MoveNext() 1670 { 1671 if ($this->EOF) return false; 1672 $this->_currentRow++; 1673 $this->fields = @mysqli_fetch_array($this->_queryID,$this->fetchMode); 1674 1675 if (is_array($this->fields)) { 1676 $this->_updatefields(); 1677 return true; 1678 } 1679 $this->EOF = true; 1680 return false; 1681 } 1682 1683 /** 1684 * Attempt to fetch a result row using the current fetch mode and return whether or not this was successful. 1685 * 1686 * @return bool True if row was fetched successfully, otherwise false. 1687 */ 1688 function _fetch() 1689 { 1690 $this->fields = mysqli_fetch_array($this->_queryID,$this->fetchMode); 1691 $this->_updatefields(); 1692 return is_array($this->fields); 1693 } 1694 1695 /** 1696 * Frees the memory associated with a result. 1697 * 1698 * @return void 1699 */ 1700 function _close() 1701 { 1702 //if results are attached to this pointer from Stored Procedure calls, the next standard query will die 2014 1703 //only a problem with persistent connections 1704 1705 if (isset($this->connection->_connectionID) && $this->connection->_connectionID) { 1706 while (mysqli_more_results($this->connection->_connectionID)) { 1707 mysqli_next_result($this->connection->_connectionID); 1708 } 1709 } 1710 1711 if ($this->_queryID instanceof mysqli_result) { 1712 mysqli_free_result($this->_queryID); 1713 } 1714 $this->_queryID = false; 1715 } 1716 1717 /* 1718 1719 0 = MYSQLI_TYPE_DECIMAL 1720 1 = MYSQLI_TYPE_CHAR 1721 1 = MYSQLI_TYPE_TINY 1722 2 = MYSQLI_TYPE_SHORT 1723 3 = MYSQLI_TYPE_LONG 1724 4 = MYSQLI_TYPE_FLOAT 1725 5 = MYSQLI_TYPE_DOUBLE 1726 6 = MYSQLI_TYPE_NULL 1727 7 = MYSQLI_TYPE_TIMESTAMP 1728 8 = MYSQLI_TYPE_LONGLONG 1729 9 = MYSQLI_TYPE_INT24 1730 10 = MYSQLI_TYPE_DATE 1731 11 = MYSQLI_TYPE_TIME 1732 12 = MYSQLI_TYPE_DATETIME 1733 13 = MYSQLI_TYPE_YEAR 1734 14 = MYSQLI_TYPE_NEWDATE 1735 245 = MYSQLI_TYPE_JSON 1736 247 = MYSQLI_TYPE_ENUM 1737 248 = MYSQLI_TYPE_SET 1738 249 = MYSQLI_TYPE_TINY_BLOB 1739 250 = MYSQLI_TYPE_MEDIUM_BLOB 1740 251 = MYSQLI_TYPE_LONG_BLOB 1741 252 = MYSQLI_TYPE_BLOB 1742 253 = MYSQLI_TYPE_VAR_STRING 1743 254 = MYSQLI_TYPE_STRING 1744 255 = MYSQLI_TYPE_GEOMETRY 1745 */ 1746 1747 /** 1748 * Get the MetaType character for a given field type. 1749 * 1750 * @param string|object $t The type to get the MetaType character for. 1751 * @param int $len (Optional) Redundant. Will always be set to -1. 1752 * @param bool|object $fieldobj (Optional) 1753 * 1754 * @return string The MetaType 1755 */ 1756 function metaType($t, $len = -1, $fieldobj = false) 1757 { 1758 if (is_object($t)) { 1759 $fieldobj = $t; 1760 $t = $fieldobj->type; 1761 $len = $fieldobj->max_length; 1762 } 1763 1764 $t = strtoupper($t); 1765 /* 1766 * Add support for custom actual types. We do this 1767 * first, that allows us to override existing types 1768 */ 1769 if (array_key_exists($t,$this->connection->customActualTypes)) 1770 return $this->connection->customActualTypes[$t]; 1771 1772 $len = -1; // mysql max_length is not accurate 1773 switch ($t) { 1774 case 'STRING': 1775 case 'CHAR': 1776 case 'VARCHAR': 1777 case 'TINYBLOB': 1778 case 'TINYTEXT': 1779 case 'ENUM': 1780 case 'SET': 1781 1782 case MYSQLI_TYPE_TINY_BLOB : 1783 // case MYSQLI_TYPE_CHAR : 1784 case MYSQLI_TYPE_STRING : 1785 case MYSQLI_TYPE_ENUM : 1786 case MYSQLI_TYPE_SET : 1787 case 253 : 1788 if ($len <= $this->blobSize) { 1789 return 'C'; 1790 } 1791 1792 case 'TEXT': 1793 case 'LONGTEXT': 1794 case 'MEDIUMTEXT': 1795 return 'X'; 1796 1797 // php_mysql extension always returns 'blob' even if 'text' 1798 // so we have to check whether binary... 1799 case 'IMAGE': 1800 case 'LONGBLOB': 1801 case 'BLOB': 1802 case 'MEDIUMBLOB': 1803 1804 case MYSQLI_TYPE_BLOB : 1805 case MYSQLI_TYPE_LONG_BLOB : 1806 case MYSQLI_TYPE_MEDIUM_BLOB : 1807 return !empty($fieldobj->binary) ? 'B' : 'X'; 1808 1809 case 'YEAR': 1810 case 'DATE': 1811 case MYSQLI_TYPE_DATE : 1812 case MYSQLI_TYPE_YEAR : 1813 return 'D'; 1814 1815 case 'TIME': 1816 case 'DATETIME': 1817 case 'TIMESTAMP': 1818 1819 case MYSQLI_TYPE_DATETIME : 1820 case MYSQLI_TYPE_NEWDATE : 1821 case MYSQLI_TYPE_TIME : 1822 case MYSQLI_TYPE_TIMESTAMP : 1823 return 'T'; 1824 1825 case 'INT': 1826 case 'INTEGER': 1827 case 'BIGINT': 1828 case 'TINYINT': 1829 case 'MEDIUMINT': 1830 case 'SMALLINT': 1831 1832 case MYSQLI_TYPE_INT24 : 1833 case MYSQLI_TYPE_LONG : 1834 case MYSQLI_TYPE_LONGLONG : 1835 case MYSQLI_TYPE_SHORT : 1836 case MYSQLI_TYPE_TINY : 1837 if (!empty($fieldobj->primary_key)) { 1838 return 'R'; 1839 } 1840 return 'I'; 1841 1842 // Added floating-point types 1843 // Maybe not necessary. 1844 case 'FLOAT': 1845 case 'DOUBLE': 1846 // case 'DOUBLE PRECISION': 1847 case 'DECIMAL': 1848 case 'DEC': 1849 case 'FIXED': 1850 default: 1851 1852 1853 //if (!is_numeric($t)) echo "<p>--- Error in type matching $t -----</p>"; 1854 return 'N'; 1855 } 1856 } 1857 1858 1859 } // rs class 1860 1861 /** 1862 * Class ADORecordSet_array_mysqli 1863 */ 1864 class ADORecordSet_array_mysqli extends ADORecordSet_array 1865 { 1866 /** 1867 * Get the MetaType character for a given field type. 1868 * 1869 * @param string|object $t The type to get the MetaType character for. 1870 * @param int $len (Optional) Redundant. Will always be set to -1. 1871 * @param bool|object $fieldobj (Optional) 1872 * 1873 * @return string The MetaType 1874 */ 1875 function MetaType($t, $len = -1, $fieldobj = false) 1876 { 1877 if (is_object($t)) { 1878 $fieldobj = $t; 1879 $t = $fieldobj->type; 1880 $len = $fieldobj->max_length; 1881 } 1882 1883 $t = strtoupper($t); 1884 1885 if (array_key_exists($t,$this->connection->customActualTypes)) 1886 return $this->connection->customActualTypes[$t]; 1887 1888 $len = -1; // mysql max_length is not accurate 1889 1890 switch ($t) { 1891 case 'STRING': 1892 case 'CHAR': 1893 case 'VARCHAR': 1894 case 'TINYBLOB': 1895 case 'TINYTEXT': 1896 case 'ENUM': 1897 case 'SET': 1898 1899 case MYSQLI_TYPE_TINY_BLOB : 1900 // case MYSQLI_TYPE_CHAR : 1901 case MYSQLI_TYPE_STRING : 1902 case MYSQLI_TYPE_ENUM : 1903 case MYSQLI_TYPE_SET : 1904 case 253 : 1905 if ($len <= $this->blobSize) { 1906 return 'C'; 1907 } 1908 1909 case 'TEXT': 1910 case 'LONGTEXT': 1911 case 'MEDIUMTEXT': 1912 return 'X'; 1913 1914 // php_mysql extension always returns 'blob' even if 'text' 1915 // so we have to check whether binary... 1916 case 'IMAGE': 1917 case 'LONGBLOB': 1918 case 'BLOB': 1919 case 'MEDIUMBLOB': 1920 1921 case MYSQLI_TYPE_BLOB : 1922 case MYSQLI_TYPE_LONG_BLOB : 1923 case MYSQLI_TYPE_MEDIUM_BLOB : 1924 return !empty($fieldobj->binary) ? 'B' : 'X'; 1925 1926 case 'YEAR': 1927 case 'DATE': 1928 case MYSQLI_TYPE_DATE : 1929 case MYSQLI_TYPE_YEAR : 1930 return 'D'; 1931 1932 case 'TIME': 1933 case 'DATETIME': 1934 case 'TIMESTAMP': 1935 1936 case MYSQLI_TYPE_DATETIME : 1937 case MYSQLI_TYPE_NEWDATE : 1938 case MYSQLI_TYPE_TIME : 1939 case MYSQLI_TYPE_TIMESTAMP : 1940 return 'T'; 1941 1942 case 'INT': 1943 case 'INTEGER': 1944 case 'BIGINT': 1945 case 'TINYINT': 1946 case 'MEDIUMINT': 1947 case 'SMALLINT': 1948 1949 case MYSQLI_TYPE_INT24 : 1950 case MYSQLI_TYPE_LONG : 1951 case MYSQLI_TYPE_LONGLONG : 1952 case MYSQLI_TYPE_SHORT : 1953 case MYSQLI_TYPE_TINY : 1954 if (!empty($fieldobj->primary_key)) { 1955 return 'R'; 1956 } 1957 return 'I'; 1958 1959 // Added floating-point types 1960 // Maybe not necessary. 1961 case 'FLOAT': 1962 case 'DOUBLE': 1963 // case 'DOUBLE PRECISION': 1964 case 'DECIMAL': 1965 case 'DEC': 1966 case 'FIXED': 1967 default: 1968 //if (!is_numeric($t)) echo "<p>--- Error in type matching $t -----</p>"; 1969 return 'N'; 1970 } 1971 } 1972 } 1973 1974 } // if defined _ADODB_MYSQLI_LAYER
title
Description
Body
title
Description
Body
title
Description
Body
title
Body