Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [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->database}' 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 1021 if ($this->_connectionID) { 1022 $result = @mysqli_select_db($this->_connectionID, $dbName); 1023 if (!$result) { 1024 ADOConnection::outp("Select of database " . $dbName . " failed. " . $this->errorMsg()); 1025 } 1026 return $result; 1027 } 1028 return false; 1029 } 1030 1031 /** 1032 * Executes a provided SQL statement and returns a handle to the result, with the ability to supply a starting 1033 * offset and record count. 1034 * 1035 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:selectlimit 1036 * 1037 * @param string $sql The SQL to execute. 1038 * @param int $nrows (Optional) The limit for the number of records you want returned. By default, all results. 1039 * @param int $offset (Optional) The offset to use when selecting the results. By default, no offset. 1040 * @param array|bool $inputarr (Optional) Any parameter values required by the SQL statement, or false if none. 1041 * @param int $secs2cache (Optional) If greater than 0, perform a cached execute. By default, normal execution. 1042 * 1043 * @return ADORecordSet|false The query results, or false if the query failed to execute. 1044 */ 1045 function SelectLimit($sql, 1046 $nrows = -1, 1047 $offset = -1, 1048 $inputarr = false, 1049 $secs2cache = 0) 1050 { 1051 $nrows = (int) $nrows; 1052 $offset = (int) $offset; 1053 $offsetStr = ($offset >= 0) ? "$offset," : ''; 1054 if ($nrows < 0) $nrows = '18446744073709551615'; 1055 1056 if ($secs2cache) 1057 $rs = $this->cacheExecute($secs2cache, $sql . " LIMIT $offsetStr$nrows" , $inputarr ); 1058 else 1059 $rs = $this->execute($sql . " LIMIT $offsetStr$nrows" , $inputarr ); 1060 1061 return $rs; 1062 } 1063 1064 /** 1065 * Prepares an SQL statement and returns a handle to use. 1066 * This is not used by bound parameters anymore 1067 * 1068 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:prepare 1069 * @todo update this function to handle prepared statements correctly 1070 * 1071 * @param string $sql The SQL to prepare. 1072 * 1073 * @return string The original SQL that was provided. 1074 */ 1075 function Prepare($sql) 1076 { 1077 /* 1078 * Flag the insert_id method to use the correct retrieval method 1079 */ 1080 $this->usePreparedStatement = true; 1081 1082 /* 1083 * Prepared statements are not yet handled correctly 1084 */ 1085 return $sql; 1086 $stmt = $this->_connectionID->prepare($sql); 1087 if (!$stmt) { 1088 echo $this->errorMsg(); 1089 return $sql; 1090 } 1091 return array($sql,$stmt); 1092 } 1093 1094 /** 1095 * Execute SQL 1096 * 1097 * @param string $sql SQL statement to execute, or possibly an array 1098 * holding prepared statement ($sql[0] will hold sql text) 1099 * @param array|bool $inputarr holds the input data to bind to. 1100 * Null elements will be set to null. 1101 * 1102 * @return ADORecordSet|bool 1103 */ 1104 public function execute($sql, $inputarr = false) 1105 { 1106 if ($this->fnExecute) { 1107 $fn = $this->fnExecute; 1108 $ret = $fn($this, $sql, $inputarr); 1109 if (isset($ret)) { 1110 return $ret; 1111 } 1112 } 1113 1114 if ($inputarr === false || $inputarr === []) { 1115 return $this->_execute($sql); 1116 } 1117 1118 if (!is_array($inputarr)) { 1119 $inputarr = array($inputarr); 1120 } 1121 else { 1122 //remove alphanumeric placeholders 1123 $inputarr = array_values($inputarr); 1124 } 1125 1126 if (!is_array($sql)) { 1127 // Check if we are bulkbinding. If so, $inputarr is a 2d array, 1128 // and we make a gross assumption that all rows have the same number 1129 // of columns of the same type, and use the elements of the first row 1130 // to determine the MySQL bind param types. 1131 if (is_array($inputarr[0])) { 1132 if (!$this->bulkBind) { 1133 $this->outp_throw( 1134 "2D Array of values sent to execute and 'ADOdb_mysqli::bulkBind' not set", 1135 'Execute' 1136 ); 1137 return false; 1138 } 1139 1140 $bulkTypeArray = []; 1141 foreach ($inputarr as $v) { 1142 if (is_string($this->bulkBind)) { 1143 $typeArray = array_merge((array)$this->bulkBind, $v); 1144 } else { 1145 $typeArray = $this->getBindParamWithType($v); 1146 } 1147 $bulkTypeArray[] = $typeArray; 1148 } 1149 $this->bulkBind = false; 1150 $ret = $this->_execute($sql, $bulkTypeArray); 1151 } else { 1152 $typeArray = $this->getBindParamWithType($inputarr); 1153 $ret = $this->_execute($sql, $typeArray); 1154 } 1155 } else { 1156 $ret = $this->_execute($sql, $inputarr); 1157 } 1158 return $ret; 1159 } 1160 1161 /** 1162 * Inserts the bind param type string at the front of the parameter array. 1163 * 1164 * @see https://www.php.net/manual/en/mysqli-stmt.bind-param.php 1165 * 1166 * @param array $inputArr 1167 * @return array 1168 */ 1169 private function getBindParamWithType($inputArr): array 1170 { 1171 $typeString = ''; 1172 foreach ($inputArr as $v) { 1173 if (is_integer($v) || is_bool($v)) { 1174 $typeString .= 'i'; 1175 } elseif (is_float($v)) { 1176 $typeString .= 'd'; 1177 } elseif (is_object($v)) { 1178 // Assume a blob 1179 $typeString .= 'b'; 1180 } else { 1181 $typeString .= 's'; 1182 } 1183 } 1184 1185 // Place the field type list at the front of the parameter array. 1186 // This is the mysql specific format 1187 array_unshift($inputArr, $typeString); 1188 return $inputArr; 1189 } 1190 1191 /** 1192 * Return the query id. 1193 * 1194 * @param string|array $sql 1195 * @param array $inputarr 1196 * 1197 * @return bool|mysqli_result 1198 */ 1199 function _query($sql, $inputarr) 1200 { 1201 global $ADODB_COUNTRECS; 1202 // Move to the next recordset, or return false if there is none. In a stored proc 1203 // call, mysqli_next_result returns true for the last "recordset", but mysqli_store_result 1204 // returns false. I think this is because the last "recordset" is actually just the 1205 // return value of the stored proc (ie the number of rows affected). 1206 // Commented out for reasons of performance. You should retrieve every recordset yourself. 1207 // if (!mysqli_next_result($this->connection->_connectionID)) return false; 1208 1209 // When SQL is empty, mysqli_query() throws exception on PHP 8 (#945) 1210 if (!$sql) { 1211 if ($this->debug) { 1212 ADOConnection::outp("Empty query"); 1213 } 1214 return false; 1215 } 1216 1217 if (is_array($sql)) { 1218 1219 // Prepare() not supported because mysqli_stmt_execute does not return a recordset, but 1220 // returns as bound variables. 1221 1222 $stmt = $sql[1]; 1223 $a = ''; 1224 foreach($inputarr as $v) { 1225 if (is_string($v)) $a .= 's'; 1226 else if (is_integer($v)) $a .= 'i'; 1227 else $a .= 'd'; 1228 } 1229 1230 /* 1231 * set prepared statement flags 1232 */ 1233 if ($this->usePreparedStatement) 1234 $this->useLastInsertStatement = true; 1235 1236 $fnarr = array_merge( array($stmt,$a) , $inputarr); 1237 call_user_func_array('mysqli_stmt_bind_param',$fnarr); 1238 return mysqli_stmt_execute($stmt); 1239 } 1240 else if (is_string($sql) && is_array($inputarr)) 1241 { 1242 1243 /* 1244 * This is support for true prepared queries 1245 * with bound parameters 1246 * 1247 * set prepared statement flags 1248 */ 1249 $this->usePreparedStatement = true; 1250 $this->usingBoundVariables = true; 1251 1252 $bulkBindArray = array(); 1253 if (is_array($inputarr[0])) 1254 { 1255 $bulkBindArray = $inputarr; 1256 $inputArrayCount = count($inputarr[0]) - 1; 1257 } 1258 else 1259 { 1260 $bulkBindArray[] = $inputarr; 1261 $inputArrayCount = count($inputarr) - 1; 1262 } 1263 1264 1265 /* 1266 * Prepare the statement with the placeholders, 1267 * prepare will fail if the statement is invalid 1268 * so we trap and error if necessary. Note that we 1269 * are calling MySQL prepare here, not ADOdb 1270 */ 1271 $stmt = $this->_connectionID->prepare($sql); 1272 if ($stmt === false) 1273 { 1274 $this->outp_throw( 1275 "SQL Statement failed on preparation: " . htmlspecialchars($sql) . "'", 1276 'Execute' 1277 ); 1278 return false; 1279 } 1280 /* 1281 * Make sure the number of parameters provided in the input 1282 * array matches what the query expects. We must discount 1283 * the first parameter which contains the data types in 1284 * our inbound parameters 1285 */ 1286 $nparams = $stmt->param_count; 1287 1288 if ($nparams != $inputArrayCount) 1289 { 1290 1291 $this->outp_throw( 1292 "Input array has " . $inputArrayCount . 1293 " params, does not match query: '" . htmlspecialchars($sql) . "'", 1294 'Execute' 1295 ); 1296 return false; 1297 } 1298 1299 foreach ($bulkBindArray as $inputarr) 1300 { 1301 /* 1302 * Must pass references into call_user_func_array 1303 */ 1304 $paramsByReference = array(); 1305 foreach($inputarr as $key => $value) { 1306 /** @noinspection PhpArrayAccessCanBeReplacedWithForeachValueInspection */ 1307 $paramsByReference[$key] = &$inputarr[$key]; 1308 } 1309 1310 /* 1311 * Bind the params 1312 */ 1313 call_user_func_array(array($stmt, 'bind_param'), $paramsByReference); 1314 1315 /* 1316 * Execute 1317 */ 1318 1319 $ret = mysqli_stmt_execute($stmt); 1320 1321 // Store error code and message 1322 $this->_errorCode = $stmt->errno; 1323 $this->_errorMsg = $stmt->error; 1324 1325 /* 1326 * Did we throw an error? 1327 */ 1328 if ($ret == false) 1329 return false; 1330 } 1331 1332 // Tells affected_rows to be compliant 1333 $this->isSelectStatement = $stmt->affected_rows == -1; 1334 if (!$this->isSelectStatement) { 1335 $this->statementAffectedRows = $stmt->affected_rows; 1336 return true; 1337 } 1338 1339 // Turn the statement into a result set and return it 1340 return $stmt->get_result(); 1341 } 1342 else 1343 { 1344 /* 1345 * reset prepared statement flags, in case we set them 1346 * previously and didn't use them 1347 */ 1348 $this->usePreparedStatement = false; 1349 $this->useLastInsertStatement = false; 1350 1351 // Reset error code and message 1352 $this->_errorCode = 0; 1353 $this->_errorMsg = ''; 1354 } 1355 1356 /* 1357 if (!$mysql_res = mysqli_query($this->_connectionID, $sql, ($ADODB_COUNTRECS) ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT)) { 1358 if ($this->debug) ADOConnection::outp("Query: " . $sql . " failed. " . $this->errorMsg()); 1359 return false; 1360 } 1361 1362 return $mysql_res; 1363 */ 1364 1365 if ($this->multiQuery) { 1366 $rs = mysqli_multi_query($this->_connectionID, $sql.';'); 1367 if ($rs) { 1368 $rs = ($ADODB_COUNTRECS) ? @mysqli_store_result( $this->_connectionID ) : @mysqli_use_result( $this->_connectionID ); 1369 return $rs ?: true; // mysqli_more_results( $this->_connectionID ) 1370 } 1371 } else { 1372 $rs = mysqli_query($this->_connectionID, $sql, $ADODB_COUNTRECS ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); 1373 if ($rs) { 1374 $this->isSelectStatement = is_object($rs); 1375 return $rs; 1376 } 1377 } 1378 1379 if($this->debug) 1380 ADOConnection::outp("Query: " . $sql . " failed. " . $this->errorMsg()); 1381 1382 return false; 1383 1384 } 1385 1386 /** 1387 * Returns a database specific error message. 1388 * 1389 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:errormsg 1390 * 1391 * @return string The last error message. 1392 */ 1393 function ErrorMsg() 1394 { 1395 if (!$this->_errorMsg) { 1396 if (empty($this->_connectionID)) { 1397 $this->_errorMsg = mysqli_connect_error(); 1398 } else { 1399 $this->_errorMsg = $this->_connectionID->error ?? $this->_connectionID->connect_error; 1400 } 1401 } 1402 return $this->_errorMsg; 1403 } 1404 1405 /** 1406 * Returns the last error number from previous database operation. 1407 * 1408 * @return int The last error number. 1409 */ 1410 function ErrorNo() 1411 { 1412 if (!$this->_errorCode) { 1413 if (empty($this->_connectionID)) { 1414 $this->_errorCode = mysqli_connect_errno(); 1415 } else { 1416 $this->_errorCode = $this->_connectionID->errno ?? $this->_connectionID->connect_errno; 1417 } 1418 } 1419 return $this->_errorCode; 1420 } 1421 1422 /** 1423 * Close the database connection. 1424 * 1425 * @return void 1426 */ 1427 function _close() 1428 { 1429 if($this->_connectionID) { 1430 mysqli_close($this->_connectionID); 1431 } 1432 $this->_connectionID = false; 1433 } 1434 1435 /** 1436 * Returns the largest length of data that can be inserted into a character field. 1437 * 1438 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:charmax 1439 * 1440 * @return int 1441 */ 1442 function CharMax() 1443 { 1444 return 255; 1445 } 1446 1447 /** 1448 * Returns the largest length of data that can be inserted into a text field. 1449 * 1450 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:textmax 1451 * 1452 * @return int 1453 */ 1454 function TextMax() 1455 { 1456 return 4294967295; 1457 } 1458 1459 function getCharSet() 1460 { 1461 if (!$this->_connectionID || !method_exists($this->_connectionID,'character_set_name')) { 1462 return false; 1463 } 1464 1465 $this->charSet = $this->_connectionID->character_set_name(); 1466 return $this->charSet ?: false; 1467 } 1468 1469 function setCharSet($charset) 1470 { 1471 if (!$this->_connectionID || !method_exists($this->_connectionID,'set_charset')) { 1472 return false; 1473 } 1474 1475 if ($this->charSet !== $charset) { 1476 if (!$this->_connectionID->set_charset($charset)) { 1477 return false; 1478 } 1479 $this->getCharSet(); 1480 } 1481 return true; 1482 } 1483 1484 } 1485 1486 /** 1487 * Class ADORecordSet_mysqli 1488 */ 1489 class ADORecordSet_mysqli extends ADORecordSet{ 1490 1491 var $databaseType = "mysqli"; 1492 var $canSeek = true; 1493 1494 /** @var ADODB_mysqli The parent connection */ 1495 var $connection = false; 1496 1497 /** @var mysqli_result result link identifier */ 1498 var $_queryID; 1499 1500 function __construct($queryID, $mode = false) 1501 { 1502 if ($mode === false) { 1503 global $ADODB_FETCH_MODE; 1504 $mode = $ADODB_FETCH_MODE; 1505 } 1506 1507 switch ($mode) { 1508 case ADODB_FETCH_NUM: 1509 $this->fetchMode = MYSQLI_NUM; 1510 break; 1511 case ADODB_FETCH_ASSOC: 1512 $this->fetchMode = MYSQLI_ASSOC; 1513 break; 1514 case ADODB_FETCH_DEFAULT: 1515 case ADODB_FETCH_BOTH: 1516 default: 1517 $this->fetchMode = MYSQLI_BOTH; 1518 break; 1519 } 1520 $this->adodbFetchMode = $mode; 1521 parent::__construct($queryID); 1522 } 1523 1524 function _initrs() 1525 { 1526 global $ADODB_COUNTRECS; 1527 1528 $this->_numOfRows = $ADODB_COUNTRECS ? @mysqli_num_rows($this->_queryID) : -1; 1529 $this->_numOfFields = @mysqli_num_fields($this->_queryID); 1530 } 1531 1532 /* 1533 1 = MYSQLI_NOT_NULL_FLAG 1534 2 = MYSQLI_PRI_KEY_FLAG 1535 4 = MYSQLI_UNIQUE_KEY_FLAG 1536 8 = MYSQLI_MULTIPLE_KEY_FLAG 1537 16 = MYSQLI_BLOB_FLAG 1538 32 = MYSQLI_UNSIGNED_FLAG 1539 64 = MYSQLI_ZEROFILL_FLAG 1540 128 = MYSQLI_BINARY_FLAG 1541 256 = MYSQLI_ENUM_FLAG 1542 512 = MYSQLI_AUTO_INCREMENT_FLAG 1543 1024 = MYSQLI_TIMESTAMP_FLAG 1544 2048 = MYSQLI_SET_FLAG 1545 32768 = MYSQLI_NUM_FLAG 1546 16384 = MYSQLI_PART_KEY_FLAG 1547 32768 = MYSQLI_GROUP_FLAG 1548 65536 = MYSQLI_UNIQUE_FLAG 1549 131072 = MYSQLI_BINCMP_FLAG 1550 */ 1551 1552 /** 1553 * Returns raw, database specific information about a field. 1554 * 1555 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:recordset:fetchfield 1556 * 1557 * @param int $fieldOffset (Optional) The field number to get information for. 1558 * 1559 * @return ADOFieldObject|bool 1560 */ 1561 function FetchField($fieldOffset = -1) 1562 { 1563 $fieldnr = $fieldOffset; 1564 if ($fieldOffset != -1) { 1565 $fieldOffset = @mysqli_field_seek($this->_queryID, $fieldnr); 1566 } 1567 $o = @mysqli_fetch_field($this->_queryID); 1568 if (!$o) return false; 1569 1570 //Fix for HHVM 1571 if ( !isset($o->flags) ) { 1572 $o->flags = 0; 1573 } 1574 /* Properties of an ADOFieldObject as set by MetaColumns */ 1575 $o->primary_key = $o->flags & MYSQLI_PRI_KEY_FLAG; 1576 $o->not_null = $o->flags & MYSQLI_NOT_NULL_FLAG; 1577 $o->auto_increment = $o->flags & MYSQLI_AUTO_INCREMENT_FLAG; 1578 $o->binary = $o->flags & MYSQLI_BINARY_FLAG; 1579 // $o->blob = $o->flags & MYSQLI_BLOB_FLAG; /* not returned by MetaColumns */ 1580 $o->unsigned = $o->flags & MYSQLI_UNSIGNED_FLAG; 1581 1582 /* 1583 * Trivial method to cast class to ADOfieldObject 1584 */ 1585 $a = new ADOFieldObject; 1586 foreach (get_object_vars($o) as $key => $name) 1587 $a->$key = $name; 1588 return $a; 1589 } 1590 1591 /** 1592 * Reads a row in associative mode if the recordset fetch mode is numeric. 1593 * Using this function when the fetch mode is set to ADODB_FETCH_ASSOC may produce unpredictable results. 1594 * 1595 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:getrowassoc 1596 * 1597 * @param int $upper Indicates whether the keys of the recordset should be upper case or lower case. 1598 * 1599 * @return array|bool 1600 */ 1601 function GetRowAssoc($upper = ADODB_ASSOC_CASE) 1602 { 1603 if ($this->fetchMode == MYSQLI_ASSOC && $upper == ADODB_ASSOC_CASE_LOWER) { 1604 return $this->fields; 1605 } 1606 return ADORecordSet::getRowAssoc($upper); 1607 } 1608 1609 /** 1610 * Returns a single field in a single row of the current recordset. 1611 * 1612 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:recordset:fields 1613 * 1614 * @param string $colname The name of the field to retrieve. 1615 * 1616 * @return mixed 1617 */ 1618 function Fields($colname) 1619 { 1620 if ($this->fetchMode != MYSQLI_NUM) { 1621 return @$this->fields[$colname]; 1622 } 1623 1624 if (!$this->bind) { 1625 $this->bind = array(); 1626 for ($i = 0; $i < $this->_numOfFields; $i++) { 1627 $o = $this->fetchField($i); 1628 $this->bind[strtoupper($o->name)] = $i; 1629 } 1630 } 1631 return $this->fields[$this->bind[strtoupper($colname)]]; 1632 } 1633 1634 /** 1635 * Adjusts the result pointer to an arbitrary row in the result. 1636 * 1637 * @param int $row The row to seek to. 1638 * 1639 * @return bool False if the recordset contains no rows, otherwise true. 1640 */ 1641 function _seek($row) 1642 { 1643 if ($this->_numOfRows == 0 || $row < 0) { 1644 return false; 1645 } 1646 1647 mysqli_data_seek($this->_queryID, $row); 1648 $this->EOF = false; 1649 return true; 1650 } 1651 1652 /** 1653 * In databases that allow accessing of recordsets, retrieves the next set. 1654 * 1655 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:recordset:nextrecordset 1656 * 1657 * @return bool 1658 */ 1659 function NextRecordSet() 1660 { 1661 global $ADODB_COUNTRECS; 1662 1663 mysqli_free_result($this->_queryID); 1664 $this->_queryID = -1; 1665 // Move to the next recordset, or return false if there is none. In a stored proc 1666 // call, mysqli_next_result returns true for the last "recordset", but mysqli_store_result 1667 // returns false. I think this is because the last "recordset" is actually just the 1668 // return value of the stored proc (ie the number of rows affected). 1669 if (!mysqli_next_result($this->connection->_connectionID)) { 1670 return false; 1671 } 1672 1673 // CD: There is no $this->_connectionID variable, at least in the ADO version I'm using 1674 $this->_queryID = ($ADODB_COUNTRECS) ? @mysqli_store_result($this->connection->_connectionID) 1675 : @mysqli_use_result($this->connection->_connectionID); 1676 1677 if (!$this->_queryID) { 1678 return false; 1679 } 1680 1681 $this->_inited = false; 1682 $this->bind = false; 1683 $this->_currentRow = -1; 1684 $this->init(); 1685 return true; 1686 } 1687 1688 /** 1689 * Moves the cursor to the next record of the recordset from the current position. 1690 * 1691 * @link https://adodb.org/dokuwiki/doku.php?id=v5:reference:connection:movenext 1692 * 1693 * @return bool False if there are no more records to move on to, otherwise true. 1694 */ 1695 function MoveNext() 1696 { 1697 if ($this->EOF) return false; 1698 $this->_currentRow++; 1699 $this->fields = @mysqli_fetch_array($this->_queryID,$this->fetchMode); 1700 1701 if (is_array($this->fields)) { 1702 $this->_updatefields(); 1703 return true; 1704 } 1705 $this->EOF = true; 1706 return false; 1707 } 1708 1709 /** 1710 * Attempt to fetch a result row using the current fetch mode and return whether or not this was successful. 1711 * 1712 * @return bool True if row was fetched successfully, otherwise false. 1713 */ 1714 function _fetch() 1715 { 1716 $this->fields = mysqli_fetch_array($this->_queryID,$this->fetchMode); 1717 $this->_updatefields(); 1718 return is_array($this->fields); 1719 } 1720 1721 /** 1722 * Frees the memory associated with a result. 1723 * 1724 * @return void 1725 */ 1726 function _close() 1727 { 1728 //if results are attached to this pointer from Stored Procedure calls, the next standard query will die 2014 1729 //only a problem with persistent connections 1730 1731 if (isset($this->connection->_connectionID) && $this->connection->_connectionID) { 1732 while (mysqli_more_results($this->connection->_connectionID)) { 1733 mysqli_next_result($this->connection->_connectionID); 1734 } 1735 } 1736 1737 if ($this->_queryID instanceof mysqli_result) { 1738 mysqli_free_result($this->_queryID); 1739 } 1740 $this->_queryID = false; 1741 } 1742 1743 /* 1744 1745 0 = MYSQLI_TYPE_DECIMAL 1746 1 = MYSQLI_TYPE_CHAR 1747 1 = MYSQLI_TYPE_TINY 1748 2 = MYSQLI_TYPE_SHORT 1749 3 = MYSQLI_TYPE_LONG 1750 4 = MYSQLI_TYPE_FLOAT 1751 5 = MYSQLI_TYPE_DOUBLE 1752 6 = MYSQLI_TYPE_NULL 1753 7 = MYSQLI_TYPE_TIMESTAMP 1754 8 = MYSQLI_TYPE_LONGLONG 1755 9 = MYSQLI_TYPE_INT24 1756 10 = MYSQLI_TYPE_DATE 1757 11 = MYSQLI_TYPE_TIME 1758 12 = MYSQLI_TYPE_DATETIME 1759 13 = MYSQLI_TYPE_YEAR 1760 14 = MYSQLI_TYPE_NEWDATE 1761 245 = MYSQLI_TYPE_JSON 1762 247 = MYSQLI_TYPE_ENUM 1763 248 = MYSQLI_TYPE_SET 1764 249 = MYSQLI_TYPE_TINY_BLOB 1765 250 = MYSQLI_TYPE_MEDIUM_BLOB 1766 251 = MYSQLI_TYPE_LONG_BLOB 1767 252 = MYSQLI_TYPE_BLOB 1768 253 = MYSQLI_TYPE_VAR_STRING 1769 254 = MYSQLI_TYPE_STRING 1770 255 = MYSQLI_TYPE_GEOMETRY 1771 */ 1772 1773 /** 1774 * Get the MetaType character for a given field type. 1775 * 1776 * @param string|object $t The type to get the MetaType character for. 1777 * @param int $len (Optional) Redundant. Will always be set to -1. 1778 * @param bool|object $fieldobj (Optional) 1779 * 1780 * @return string The MetaType 1781 */ 1782 function metaType($t, $len = -1, $fieldobj = false) 1783 { 1784 if (is_object($t)) { 1785 $fieldobj = $t; 1786 $t = $fieldobj->type; 1787 $len = $fieldobj->max_length; 1788 } 1789 1790 $t = strtoupper($t); 1791 /* 1792 * Add support for custom actual types. We do this 1793 * first, that allows us to override existing types 1794 */ 1795 if (array_key_exists($t,$this->connection->customActualTypes)) 1796 return $this->connection->customActualTypes[$t]; 1797 1798 $len = -1; // mysql max_length is not accurate 1799 switch ($t) { 1800 case 'STRING': 1801 case 'CHAR': 1802 case 'VARCHAR': 1803 case 'TINYBLOB': 1804 case 'TINYTEXT': 1805 case 'ENUM': 1806 case 'SET': 1807 1808 case MYSQLI_TYPE_TINY_BLOB : 1809 // case MYSQLI_TYPE_CHAR : 1810 case MYSQLI_TYPE_STRING : 1811 case MYSQLI_TYPE_ENUM : 1812 case MYSQLI_TYPE_SET : 1813 case 253 : 1814 if ($len <= $this->blobSize) { 1815 return 'C'; 1816 } 1817 1818 case 'TEXT': 1819 case 'LONGTEXT': 1820 case 'MEDIUMTEXT': 1821 return 'X'; 1822 1823 // php_mysql extension always returns 'blob' even if 'text' 1824 // so we have to check whether binary... 1825 case 'IMAGE': 1826 case 'LONGBLOB': 1827 case 'BLOB': 1828 case 'MEDIUMBLOB': 1829 1830 case MYSQLI_TYPE_BLOB : 1831 case MYSQLI_TYPE_LONG_BLOB : 1832 case MYSQLI_TYPE_MEDIUM_BLOB : 1833 return !empty($fieldobj->binary) ? 'B' : 'X'; 1834 1835 case 'YEAR': 1836 case 'DATE': 1837 case MYSQLI_TYPE_DATE : 1838 case MYSQLI_TYPE_YEAR : 1839 return 'D'; 1840 1841 case 'TIME': 1842 case 'DATETIME': 1843 case 'TIMESTAMP': 1844 1845 case MYSQLI_TYPE_DATETIME : 1846 case MYSQLI_TYPE_NEWDATE : 1847 case MYSQLI_TYPE_TIME : 1848 case MYSQLI_TYPE_TIMESTAMP : 1849 return 'T'; 1850 1851 case 'INT': 1852 case 'INTEGER': 1853 case 'BIGINT': 1854 case 'TINYINT': 1855 case 'MEDIUMINT': 1856 case 'SMALLINT': 1857 1858 case MYSQLI_TYPE_INT24 : 1859 case MYSQLI_TYPE_LONG : 1860 case MYSQLI_TYPE_LONGLONG : 1861 case MYSQLI_TYPE_SHORT : 1862 case MYSQLI_TYPE_TINY : 1863 if (!empty($fieldobj->primary_key)) { 1864 return 'R'; 1865 } 1866 return 'I'; 1867 1868 // Added floating-point types 1869 // Maybe not necessary. 1870 case 'FLOAT': 1871 case 'DOUBLE': 1872 // case 'DOUBLE PRECISION': 1873 case 'DECIMAL': 1874 case 'DEC': 1875 case 'FIXED': 1876 default: 1877 1878 1879 //if (!is_numeric($t)) echo "<p>--- Error in type matching $t -----</p>"; 1880 return 'N'; 1881 } 1882 } 1883 1884 1885 } // rs class 1886 1887 /** 1888 * Class ADORecordSet_array_mysqli 1889 */ 1890 class ADORecordSet_array_mysqli extends ADORecordSet_array 1891 { 1892 /** 1893 * Get the MetaType character for a given field type. 1894 * 1895 * @param string|object $t The type to get the MetaType character for. 1896 * @param int $len (Optional) Redundant. Will always be set to -1. 1897 * @param bool|object $fieldobj (Optional) 1898 * 1899 * @return string The MetaType 1900 */ 1901 function MetaType($t, $len = -1, $fieldobj = false) 1902 { 1903 if (is_object($t)) { 1904 $fieldobj = $t; 1905 $t = $fieldobj->type; 1906 $len = $fieldobj->max_length; 1907 } 1908 1909 $t = strtoupper($t); 1910 1911 if (array_key_exists($t,$this->connection->customActualTypes)) 1912 return $this->connection->customActualTypes[$t]; 1913 1914 $len = -1; // mysql max_length is not accurate 1915 1916 switch ($t) { 1917 case 'STRING': 1918 case 'CHAR': 1919 case 'VARCHAR': 1920 case 'TINYBLOB': 1921 case 'TINYTEXT': 1922 case 'ENUM': 1923 case 'SET': 1924 1925 case MYSQLI_TYPE_TINY_BLOB : 1926 // case MYSQLI_TYPE_CHAR : 1927 case MYSQLI_TYPE_STRING : 1928 case MYSQLI_TYPE_ENUM : 1929 case MYSQLI_TYPE_SET : 1930 case 253 : 1931 if ($len <= $this->blobSize) { 1932 return 'C'; 1933 } 1934 1935 case 'TEXT': 1936 case 'LONGTEXT': 1937 case 'MEDIUMTEXT': 1938 return 'X'; 1939 1940 // php_mysql extension always returns 'blob' even if 'text' 1941 // so we have to check whether binary... 1942 case 'IMAGE': 1943 case 'LONGBLOB': 1944 case 'BLOB': 1945 case 'MEDIUMBLOB': 1946 1947 case MYSQLI_TYPE_BLOB : 1948 case MYSQLI_TYPE_LONG_BLOB : 1949 case MYSQLI_TYPE_MEDIUM_BLOB : 1950 return !empty($fieldobj->binary) ? 'B' : 'X'; 1951 1952 case 'YEAR': 1953 case 'DATE': 1954 case MYSQLI_TYPE_DATE : 1955 case MYSQLI_TYPE_YEAR : 1956 return 'D'; 1957 1958 case 'TIME': 1959 case 'DATETIME': 1960 case 'TIMESTAMP': 1961 1962 case MYSQLI_TYPE_DATETIME : 1963 case MYSQLI_TYPE_NEWDATE : 1964 case MYSQLI_TYPE_TIME : 1965 case MYSQLI_TYPE_TIMESTAMP : 1966 return 'T'; 1967 1968 case 'INT': 1969 case 'INTEGER': 1970 case 'BIGINT': 1971 case 'TINYINT': 1972 case 'MEDIUMINT': 1973 case 'SMALLINT': 1974 1975 case MYSQLI_TYPE_INT24 : 1976 case MYSQLI_TYPE_LONG : 1977 case MYSQLI_TYPE_LONGLONG : 1978 case MYSQLI_TYPE_SHORT : 1979 case MYSQLI_TYPE_TINY : 1980 if (!empty($fieldobj->primary_key)) { 1981 return 'R'; 1982 } 1983 return 'I'; 1984 1985 // Added floating-point types 1986 // Maybe not necessary. 1987 case 'FLOAT': 1988 case 'DOUBLE': 1989 // case 'DOUBLE PRECISION': 1990 case 'DECIMAL': 1991 case 'DEC': 1992 case 'FIXED': 1993 default: 1994 //if (!is_numeric($t)) echo "<p>--- Error in type matching $t -----</p>"; 1995 return 'N'; 1996 } 1997 } 1998 } 1999 2000 } // if defined _ADODB_MYSQLI_LAYER
title
Description
Body
title
Description
Body
title
Description
Body
title
Body