Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  /*
   3  @version   v5.20.16  12-Jan-2020
   4  @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
   5  @copyright (c) 2014      Damien Regad, Mark Newnham and the ADOdb community
   6    Released under both BSD license and Lesser GPL library license.
   7    Whenever there is any discrepancy between the two licenses,
   8    the BSD license will take precedence.
   9  Set tabs to 4 for best viewing.
  10  
  11    Latest version is available at http://adodb.org/
  12  
  13    Requires ODBC. Works on Windows and Unix.
  14  */
  15  // security - hide paths
  16  if (!defined('ADODB_DIR')) die();
  17  
  18    define("_ADODB_ODBC_LAYER", 2 );
  19  
  20  /*--------------------------------------------------------------------------------------
  21  --------------------------------------------------------------------------------------*/
  22  
  23  
  24  class ADODB_odbc extends ADOConnection {
  25  	 var $databaseType = "odbc";
  26  	 var $fmtDate = "'Y-m-d'";
  27  	 var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
  28  	 var $replaceQuote = "''"; // string to use to replace quotes
  29  	 var $dataProvider = "odbc";
  30  	 var $hasAffectedRows = true;
  31  	 var $binmode = ODBC_BINMODE_RETURN;
  32  	 var $useFetchArray = false; // setting this to true will make array elements in FETCH_ASSOC mode case-sensitive
  33  	 	 	 	 	 	 	 	 // breaking backward-compat
  34  	 //var $longreadlen = 8000; // default number of chars to return for a Blob/Long field
  35  	 var $_bindInputArray = false;
  36  	 var $curmode = SQL_CUR_USE_DRIVER; // See sqlext.h, SQL_CUR_DEFAULT == SQL_CUR_USE_DRIVER == 2L
  37  	 var $_genSeqSQL = "create table %s (id integer)";
  38  	 var $_autocommit = true;
  39  	 var $_haserrorfunctions = true;
  40  	 var $_has_stupid_odbc_fetch_api_change = true;
  41  	 var $_lastAffectedRows = 0;
  42  	 var $uCaseTables = true; // for meta* functions, uppercase table names
  43  
  44  	function __construct()
  45  	 {
  46  	 	 $this->_haserrorfunctions = ADODB_PHPVER >= 0x4050;
  47  	 	 $this->_has_stupid_odbc_fetch_api_change = ADODB_PHPVER >= 0x4200;
  48  	 }
  49  
  50  	 	 // returns true or false
  51  	function _connect($argDSN, $argUsername, $argPassword, $argDatabasename)
  52  	 {
  53  	 	 if (!function_exists('odbc_connect')) return null;
  54  
  55  	 	 if (!empty($argDatabasename) && stristr($argDSN, 'Database=') === false) {
  56  	 	 	 $argDSN = trim($argDSN);
  57  	 	 	 $endDSN = substr($argDSN, strlen($argDSN) - 1);
  58  	 	 	 if ($endDSN != ';') $argDSN .= ';';
  59  	 	 	 $argDSN .= 'Database='.$argDatabasename;
  60  	 	 }
  61  
  62  	 	 $last_php_error = $this->resetLastError();
  63  	 	 if ($this->curmode === false) $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword);
  64  	 	 else $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword,$this->curmode);
  65  	 	 $this->_errorMsg = $this->getChangedErrorMsg($last_php_error);
  66  	 	 if (isset($this->connectStmt)) $this->Execute($this->connectStmt);
  67  
  68  	 	 return $this->_connectionID != false;
  69  	 }
  70  
  71  	 // returns true or false
  72  	function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
  73  	 {
  74  	 	 if (!function_exists('odbc_connect')) return null;
  75  
  76  	 	 $last_php_error = $this->resetLastError();
  77  	 	 $this->_errorMsg = '';
  78  	 	 if ($this->debug && $argDatabasename) {
  79  	 	 	 ADOConnection::outp("For odbc PConnect(), $argDatabasename is not used. Place dsn in 1st parameter.");
  80  	 	 }
  81  	 //	 print "dsn=$argDSN u=$argUsername p=$argPassword<br>"; flush();
  82  	 	 if ($this->curmode === false) $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword);
  83  	 	 else $this->_connectionID = odbc_pconnect($argDSN,$argUsername,$argPassword,$this->curmode);
  84  
  85  	 	 $this->_errorMsg = $this->getChangedErrorMsg($last_php_error);
  86  	 	 if ($this->_connectionID && $this->autoRollback) @odbc_rollback($this->_connectionID);
  87  	 	 if (isset($this->connectStmt)) $this->Execute($this->connectStmt);
  88  
  89  	 	 return $this->_connectionID != false;
  90  	 }
  91  
  92  
  93  	function ServerInfo()
  94  	 {
  95  
  96  	 	 if (!empty($this->host) && ADODB_PHPVER >= 0x4300) {
  97  	 	 	 $dsn = strtoupper($this->host);
  98  	 	 	 $first = true;
  99  	 	 	 $found = false;
 100  
 101  	 	 	 if (!function_exists('odbc_data_source')) return false;
 102  
 103  	 	 	 while(true) {
 104  
 105  	 	 	 	 $rez = @odbc_data_source($this->_connectionID,
 106  	 	 	 	 	 $first ? SQL_FETCH_FIRST : SQL_FETCH_NEXT);
 107  	 	 	 	 $first = false;
 108  	 	 	 	 if (!is_array($rez)) break;
 109  	 	 	 	 if (strtoupper($rez['server']) == $dsn) {
 110  	 	 	 	 	 $found = true;
 111  	 	 	 	 	 break;
 112  	 	 	 	 }
 113  	 	 	 }
 114  	 	 	 if (!$found) return ADOConnection::ServerInfo();
 115  	 	 	 if (!isset($rez['version'])) $rez['version'] = '';
 116  	 	 	 return $rez;
 117  	 	 } else {
 118  	 	 	 return ADOConnection::ServerInfo();
 119  	 	 }
 120  	 }
 121  
 122  
 123  	function CreateSequence($seqname='adodbseq',$start=1)
 124  	 {
 125  	 	 if (empty($this->_genSeqSQL)) return false;
 126  	 	 $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
 127  	 	 if (!$ok) return false;
 128  	 	 $start -= 1;
 129  	 	 return $this->Execute("insert into $seqname values($start)");
 130  	 }
 131  
 132  	 var $_dropSeqSQL = 'drop table %s';
 133  	function DropSequence($seqname = 'adodbseq')
 134  	 {
 135  	 	 if (empty($this->_dropSeqSQL)) return false;
 136  	 	 return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
 137  	 }
 138  
 139  	 /*
 140  	 	 This algorithm is not very efficient, but works even if table locking
 141  	 	 is not available.
 142  
 143  	 	 Will return false if unable to generate an ID after $MAXLOOPS attempts.
 144  	 */
 145  	function GenID($seq='adodbseq',$start=1)
 146  	 {
 147  	 	 // if you have to modify the parameter below, your database is overloaded,
 148  	 	 // or you need to implement generation of id's yourself!
 149  	 	 $MAXLOOPS = 100;
 150  	 	 //$this->debug=1;
 151  	 	 while (--$MAXLOOPS>=0) {
 152  	 	 	 $num = $this->GetOne("select id from $seq");
 153  	 	 	 if ($num === false) {
 154  	 	 	 	 $this->Execute(sprintf($this->_genSeqSQL ,$seq));
 155  	 	 	 	 $start -= 1;
 156  	 	 	 	 $num = '0';
 157  	 	 	 	 $ok = $this->Execute("insert into $seq values($start)");
 158  	 	 	 	 if (!$ok) return false;
 159  	 	 	 }
 160  	 	 	 $this->Execute("update $seq set id=id+1 where id=$num");
 161  
 162  	 	 	 if ($this->affected_rows() > 0) {
 163  	 	 	 	 $num += 1;
 164  	 	 	 	 $this->genID = $num;
 165  	 	 	 	 return $num;
 166  	 	 	 } elseif ($this->affected_rows() == 0) {
 167  	 	 	 	 // some drivers do not return a valid value => try with another method
 168  	 	 	 	 $value = $this->GetOne("select id from $seq");
 169  	 	 	 	 if ($value == $num + 1) {
 170  	 	 	 	 	 return $value;
 171  	 	 	 	 }
 172  	 	 	 }
 173  	 	 }
 174  	 	 if ($fn = $this->raiseErrorFn) {
 175  	 	 	 $fn($this->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
 176  	 	 }
 177  	 	 return false;
 178  	 }
 179  
 180  
 181  	function ErrorMsg()
 182  	 {
 183  	 	 if ($this->_haserrorfunctions) {
 184  	 	 	 if ($this->_errorMsg !== false) return $this->_errorMsg;
 185  	 	 	 if (empty($this->_connectionID)) return @odbc_errormsg();
 186  	 	 	 return @odbc_errormsg($this->_connectionID);
 187  	 	 } else return ADOConnection::ErrorMsg();
 188  	 }
 189  
 190  	function ErrorNo()
 191  	 {
 192  
 193  	 	 if ($this->_haserrorfunctions) {
 194  	 	 	 if ($this->_errorCode !== false) {
 195  	 	 	 	 // bug in 4.0.6, error number can be corrupted string (should be 6 digits)
 196  	 	 	 	 return (strlen($this->_errorCode)<=2) ? 0 : $this->_errorCode;
 197  	 	 	 }
 198  
 199  	 	 	 if (empty($this->_connectionID)) $e = @odbc_error();
 200  	 	 	 else $e = @odbc_error($this->_connectionID);
 201  
 202  	 	 	  // bug in 4.0.6, error number can be corrupted string (should be 6 digits)
 203  	 	 	  // so we check and patch
 204  	 	 	 if (strlen($e)<=2) return 0;
 205  	 	 	 return $e;
 206  	 	 } else return ADOConnection::ErrorNo();
 207  	 }
 208  
 209  
 210  
 211  	function BeginTrans()
 212  	 {
 213  	 	 if (!$this->hasTransactions) return false;
 214  	 	 if ($this->transOff) return true;
 215  	 	 $this->transCnt += 1;
 216  	 	 $this->_autocommit = false;
 217  	 	 return odbc_autocommit($this->_connectionID,false);
 218  	 }
 219  
 220  	function CommitTrans($ok=true)
 221  	 {
 222  	 	 if ($this->transOff) return true;
 223  	 	 if (!$ok) return $this->RollbackTrans();
 224  	 	 if ($this->transCnt) $this->transCnt -= 1;
 225  	 	 $this->_autocommit = true;
 226  	 	 $ret = odbc_commit($this->_connectionID);
 227  	 	 odbc_autocommit($this->_connectionID,true);
 228  	 	 return $ret;
 229  	 }
 230  
 231  	function RollbackTrans()
 232  	 {
 233  	 	 if ($this->transOff) return true;
 234  	 	 if ($this->transCnt) $this->transCnt -= 1;
 235  	 	 $this->_autocommit = true;
 236  	 	 $ret = odbc_rollback($this->_connectionID);
 237  	 	 odbc_autocommit($this->_connectionID,true);
 238  	 	 return $ret;
 239  	 }
 240  
 241  	function MetaPrimaryKeys($table,$owner=false)
 242  	 {
 243  	 global $ADODB_FETCH_MODE;
 244  
 245  	 	 if ($this->uCaseTables) $table = strtoupper($table);
 246  	 	 $schema = '';
 247  	 	 $this->_findschema($table,$schema);
 248  
 249  	 	 $savem = $ADODB_FETCH_MODE;
 250  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 251  	 	 $qid = @odbc_primarykeys($this->_connectionID,'',$schema,$table);
 252  
 253  	 	 if (!$qid) {
 254  	 	 	 $ADODB_FETCH_MODE = $savem;
 255  	 	 	 return false;
 256  	 	 }
 257  	 	 $rs = new ADORecordSet_odbc($qid);
 258  	 	 $ADODB_FETCH_MODE = $savem;
 259  
 260  	 	 if (!$rs) return false;
 261  	 	 $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change;
 262  
 263  	 	 $arr = $rs->GetArray();
 264  	 	 $rs->Close();
 265  	 	 //print_r($arr);
 266  	 	 $arr2 = array();
 267  	 	 for ($i=0; $i < sizeof($arr); $i++) {
 268  	 	 	 if ($arr[$i][3]) $arr2[] = $arr[$i][3];
 269  	 	 }
 270  	 	 return $arr2;
 271  	 }
 272  
 273  
 274  
 275  	function MetaTables($ttype=false,$showSchema=false,$mask=false)
 276  	 {
 277  	 global $ADODB_FETCH_MODE;
 278  
 279  	 	 $savem = $ADODB_FETCH_MODE;
 280  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 281  	 	 $qid = odbc_tables($this->_connectionID);
 282  
 283  	 	 $rs = new ADORecordSet_odbc($qid);
 284  
 285  	 	 $ADODB_FETCH_MODE = $savem;
 286  	 	 if (!$rs) {
 287  	 	 	 $false = false;
 288  	 	 	 return $false;
 289  	 	 }
 290  	 	 $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change;
 291  
 292  	 	 $arr = $rs->GetArray();
 293  	 	 //print_r($arr);
 294  
 295  	 	 $rs->Close();
 296  	 	 $arr2 = array();
 297  
 298  	 	 if ($ttype) {
 299  	 	 	 $isview = strncmp($ttype,'V',1) === 0;
 300  	 	 }
 301  	 	 for ($i=0; $i < sizeof($arr); $i++) {
 302  	 	 	 if (!$arr[$i][2]) continue;
 303  	 	 	 $type = $arr[$i][3];
 304  	 	 	 if ($ttype) {
 305  	 	 	 	 if ($isview) {
 306  	 	 	 	 	 if (strncmp($type,'V',1) === 0) $arr2[] = $arr[$i][2];
 307  	 	 	 	 } else if (strncmp($type,'SYS',3) !== 0) $arr2[] = $arr[$i][2];
 308  	 	 	 } else if (strncmp($type,'SYS',3) !== 0) $arr2[] = $arr[$i][2];
 309  	 	 }
 310  	 	 return $arr2;
 311  	 }
 312  
 313  /*
 314  See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcdatetime_data_type_changes.asp
 315  / SQL data type codes /
 316  #define	 SQL_UNKNOWN_TYPE	 0
 317  #define SQL_CHAR	 	 	 1
 318  #define SQL_NUMERIC	 	  2
 319  #define SQL_DECIMAL	 	  3
 320  #define SQL_INTEGER	 	  4
 321  #define SQL_SMALLINT	 	 5
 322  #define SQL_FLOAT	 	    6
 323  #define SQL_REAL	 	 	 7
 324  #define SQL_DOUBLE	 	   8
 325  #if (ODBCVER >= 0x0300)
 326  #define SQL_DATETIME	 	 9
 327  #endif
 328  #define SQL_VARCHAR	 	 12
 329  
 330  
 331  / One-parameter shortcuts for date/time data types /
 332  #if (ODBCVER >= 0x0300)
 333  #define SQL_TYPE_DATE	   91
 334  #define SQL_TYPE_TIME	   92
 335  #define SQL_TYPE_TIMESTAMP 93
 336  
 337  #define SQL_UNICODE                             (-95)
 338  #define SQL_UNICODE_VARCHAR                     (-96)
 339  #define SQL_UNICODE_LONGVARCHAR                 (-97)
 340  */
 341  	function ODBCTypes($t)
 342  	 {
 343  	 	 switch ((integer)$t) {
 344  	 	 case 1:
 345  	 	 case 12:
 346  	 	 case 0:
 347  	 	 case -95:
 348  	 	 case -96:
 349  	 	 	 return 'C';
 350  	 	 case -97:
 351  	 	 case -1: //text
 352  	 	 	 return 'X';
 353  	 	 case -4: //image
 354  	 	 	 return 'B';
 355  
 356  	 	 case 9:
 357  	 	 case 91:
 358  	 	 	 return 'D';
 359  
 360  	 	 case 10:
 361  	 	 case 11:
 362  	 	 case 92:
 363  	 	 case 93:
 364  	 	 	 return 'T';
 365  
 366  	 	 case 4:
 367  	 	 case 5:
 368  	 	 case -6:
 369  	 	 	 return 'I';
 370  
 371  	 	 case -11: // uniqidentifier
 372  	 	 	 return 'R';
 373  	 	 case -7: //bit
 374  	 	 	 return 'L';
 375  
 376  	 	 default:
 377  	 	 	 return 'N';
 378  	 	 }
 379  	 }
 380  
 381  	function MetaColumns($table, $normalize=true)
 382  	 {
 383  	 global $ADODB_FETCH_MODE;
 384  
 385  	 	 $false = false;
 386  	 	 if ($this->uCaseTables) $table = strtoupper($table);
 387  	 	 $schema = '';
 388  	 	 $this->_findschema($table,$schema);
 389  
 390  	 	 $savem = $ADODB_FETCH_MODE;
 391  	 	 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
 392  
 393  	 	 /*if (false) { // after testing, confirmed that the following does not work becoz of a bug
 394  	 	 	 $qid2 = odbc_tables($this->_connectionID);
 395  	 	 	 $rs = new ADORecordSet_odbc($qid2);
 396  	 	 	 $ADODB_FETCH_MODE = $savem;
 397  	 	 	 if (!$rs) return false;
 398  	 	 	 $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change;
 399  	 	 	 $rs->_fetch();
 400  
 401  	 	 	 while (!$rs->EOF) {
 402  	 	 	 	 if ($table == strtoupper($rs->fields[2])) {
 403  	 	 	 	 	 $q = $rs->fields[0];
 404  	 	 	 	 	 $o = $rs->fields[1];
 405  	 	 	 	 	 break;
 406  	 	 	 	 }
 407  	 	 	 	 $rs->MoveNext();
 408  	 	 	 }
 409  	 	 	 $rs->Close();
 410  
 411  	 	 	 $qid = odbc_columns($this->_connectionID,$q,$o,strtoupper($table),'%');
 412  	 	 } */
 413  
 414  	 	 switch ($this->databaseType) {
 415  	 	 case 'access':
 416  	 	 case 'vfp':
 417  	 	 	 $qid = odbc_columns($this->_connectionID);#,'%','',strtoupper($table),'%');
 418  	 	 	 break;
 419  
 420  
 421  	 	 case 'db2':
 422              $colname = "%";
 423              $qid = odbc_columns($this->_connectionID, "", $schema, $table, $colname);
 424              break;
 425  
 426  	 	 default:
 427  	 	 	 $qid = @odbc_columns($this->_connectionID,'%','%',strtoupper($table),'%');
 428  	 	 	 if (empty($qid)) $qid = odbc_columns($this->_connectionID);
 429  	 	 	 break;
 430  	 	 }
 431  	 	 if (empty($qid)) return $false;
 432  
 433  	 	 $rs = new ADORecordSet_odbc($qid);
 434  	 	 $ADODB_FETCH_MODE = $savem;
 435  
 436  	 	 if (!$rs) return $false;
 437  	 	 $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change;
 438  	 	 $rs->_fetch();
 439  
 440  	 	 $retarr = array();
 441  
 442  	 	 /*
 443  	 	 $rs->fields indices
 444  	 	 0 TABLE_QUALIFIER
 445  	 	 1 TABLE_SCHEM
 446  	 	 2 TABLE_NAME
 447  	 	 3 COLUMN_NAME
 448  	 	 4 DATA_TYPE
 449  	 	 5 TYPE_NAME
 450  	 	 6 PRECISION
 451  	 	 7 LENGTH
 452  	 	 8 SCALE
 453  	 	 9 RADIX
 454  	 	 10 NULLABLE
 455  	 	 11 REMARKS
 456  	 	 */
 457  	 	 while (!$rs->EOF) {
 458  	 	 //	 adodb_pr($rs->fields);
 459  	 	 	 if (strtoupper(trim($rs->fields[2])) == $table && (!$schema || strtoupper($rs->fields[1]) == $schema)) {
 460  	 	 	 	 $fld = new ADOFieldObject();
 461  	 	 	 	 $fld->name = $rs->fields[3];
 462  	 	 	 	 $fld->type = $this->ODBCTypes($rs->fields[4]);
 463  
 464  	 	 	 	 // ref: http://msdn.microsoft.com/library/default.asp?url=/archive/en-us/dnaraccgen/html/msdn_odk.asp
 465  	 	 	 	 // access uses precision to store length for char/varchar
 466  	 	 	 	 if ($fld->type == 'C' or $fld->type == 'X') {
 467  	 	 	 	 	 if ($this->databaseType == 'access')
 468  	 	 	 	 	 	 $fld->max_length = $rs->fields[6];
 469  	 	 	 	 	 else if ($rs->fields[4] <= -95) // UNICODE
 470  	 	 	 	 	 	 $fld->max_length = $rs->fields[7]/2;
 471  	 	 	 	 	 else
 472  	 	 	 	 	 	 $fld->max_length = $rs->fields[7];
 473  	 	 	 	 } else
 474  	 	 	 	 	 $fld->max_length = $rs->fields[7];
 475  	 	 	 	 $fld->not_null = !empty($rs->fields[10]);
 476  	 	 	 	 $fld->scale = $rs->fields[8];
 477  	 	 	 	 $retarr[strtoupper($fld->name)] = $fld;
 478  	 	 	 } else if (sizeof($retarr)>0)
 479  	 	 	 	 break;
 480  	 	 	 $rs->MoveNext();
 481  	 	 }
 482  	 	 $rs->Close(); //-- crashes 4.03pl1 -- why?
 483  
 484  	 	 if (empty($retarr)) $retarr = false;
 485  	 	 return $retarr;
 486  	 }
 487  
 488  	function Prepare($sql)
 489  	 {
 490  	 	 if (! $this->_bindInputArray) return $sql; // no binding
 491  	 	 $stmt = odbc_prepare($this->_connectionID,$sql);
 492  	 	 if (!$stmt) {
 493  	 	 	 // we don't know whether odbc driver is parsing prepared stmts, so just return sql
 494  	 	 	 return $sql;
 495  	 	 }
 496  	 	 return array($sql,$stmt,false);
 497  	 }
 498  
 499  	 /* returns queryID or false */
 500  	function _query($sql,$inputarr=false)
 501  	 {
 502  	 	 $last_php_error = $this->resetLastError();
 503  	 	 $this->_errorMsg = '';
 504  
 505  	 	 if ($inputarr) {
 506  	 	 	 if (is_array($sql)) {
 507  	 	 	 	 $stmtid = $sql[1];
 508  	 	 	 } else {
 509  	 	 	 	 $stmtid = odbc_prepare($this->_connectionID,$sql);
 510  
 511  	 	 	 	 if ($stmtid == false) {
 512  	 	 	 	 	 $this->_errorMsg = $this->getChangedErrorMsg($last_php_error);
 513  	 	 	 	 	 return false;
 514  	 	 	 	 }
 515  	 	 	 }
 516  
 517  	 	 	 if (! odbc_execute($stmtid,$inputarr)) {
 518  	 	 	 	 //@odbc_free_result($stmtid);
 519  	 	 	 	 if ($this->_haserrorfunctions) {
 520  	 	 	 	 	 $this->_errorMsg = odbc_errormsg();
 521  	 	 	 	 	 $this->_errorCode = odbc_error();
 522  	 	 	 	 }
 523  	 	 	 	 return false;
 524  	 	 	 }
 525  
 526  	 	 } else if (is_array($sql)) {
 527  	 	 	 $stmtid = $sql[1];
 528  	 	 	 if (!odbc_execute($stmtid)) {
 529  	 	 	 	 //@odbc_free_result($stmtid);
 530  	 	 	 	 if ($this->_haserrorfunctions) {
 531  	 	 	 	 	 $this->_errorMsg = odbc_errormsg();
 532  	 	 	 	 	 $this->_errorCode = odbc_error();
 533  	 	 	 	 }
 534  	 	 	 	 return false;
 535  	 	 	 }
 536  	 	 } else
 537  	 	 	 $stmtid = odbc_exec($this->_connectionID,$sql);
 538  
 539  	 	 $this->_lastAffectedRows = 0;
 540  	 	 if ($stmtid) {
 541  	 	 	 if (@odbc_num_fields($stmtid) == 0) {
 542  	 	 	 	 $this->_lastAffectedRows = odbc_num_rows($stmtid);
 543  	 	 	 	 $stmtid = true;
 544  	 	 	 } else {
 545  	 	 	 	 $this->_lastAffectedRows = 0;
 546  	 	 	 	 odbc_binmode($stmtid,$this->binmode);
 547  	 	 	 	 odbc_longreadlen($stmtid,$this->maxblobsize);
 548  	 	 	 }
 549  
 550  	 	 	 if ($this->_haserrorfunctions) {
 551  	 	 	 	 $this->_errorMsg = '';
 552  	 	 	 	 $this->_errorCode = 0;
 553  	 	 	 } else {
 554  	 	 	 	 $this->_errorMsg = $this->getChangedErrorMsg($last_php_error);
 555  	 	 	 }
 556  	 	 } else {
 557  	 	 	 if ($this->_haserrorfunctions) {
 558  	 	 	 	 $this->_errorMsg = odbc_errormsg();
 559  	 	 	 	 $this->_errorCode = odbc_error();
 560  	 	 	 } else {
 561  	 	 	 	 $this->_errorMsg = $this->getChangedErrorMsg($last_php_error);
 562  	 	 	 }
 563  	 	 }
 564  	 	 return $stmtid;
 565  	 }
 566  
 567  	 /*
 568  	 	 Insert a null into the blob field of the table first.
 569  	 	 Then use UpdateBlob to store the blob.
 570  
 571  	 	 Usage:
 572  
 573  	 	 $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
 574  	 	 $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
 575  	 */
 576  	function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
 577  	 {
 578  	 	 return $this->Execute("UPDATE $table SET $column=? WHERE $where",array($val)) != false;
 579  	 }
 580  
 581  	 // returns true or false
 582  	function _close()
 583  	 {
 584  	 	 $ret = @odbc_close($this->_connectionID);
 585  	 	 $this->_connectionID = false;
 586  	 	 return $ret;
 587  	 }
 588  
 589  	function _affectedrows()
 590  	 {
 591  	 	 return $this->_lastAffectedRows;
 592  	 }
 593  
 594  }
 595  
 596  /*--------------------------------------------------------------------------------------
 597  	  Class Name: Recordset
 598  --------------------------------------------------------------------------------------*/
 599  
 600  class ADORecordSet_odbc extends ADORecordSet {
 601  
 602  	 var $bind = false;
 603  	 var $databaseType = "odbc";
 604  	 var $dataProvider = "odbc";
 605  	 var $useFetchArray;
 606  	 var $_has_stupid_odbc_fetch_api_change;
 607  
 608  	function __construct($id,$mode=false)
 609  	 {
 610  	 	 if ($mode === false) {
 611  	 	 	 global $ADODB_FETCH_MODE;
 612  	 	 	 $mode = $ADODB_FETCH_MODE;
 613  	 	 }
 614  	 	 $this->fetchMode = $mode;
 615  
 616  	 	 $this->_queryID = $id;
 617  
 618  	 	 // the following is required for mysql odbc driver in 4.3.1 -- why?
 619  	 	 $this->EOF = false;
 620  	 	 $this->_currentRow = -1;
 621  	 	 //parent::__construct($id);
 622  	 }
 623  
 624  
 625  	 // returns the field object
 626  	function FetchField($fieldOffset = -1)
 627  	 {
 628  
 629  	 	 $off=$fieldOffset+1; // offsets begin at 1
 630  
 631  	 	 $o= new ADOFieldObject();
 632  	 	 $o->name = @odbc_field_name($this->_queryID,$off);
 633  	 	 $o->type = @odbc_field_type($this->_queryID,$off);
 634  	 	 $o->max_length = @odbc_field_len($this->_queryID,$off);
 635  	 	 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
 636  	 	 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
 637  	 	 return $o;
 638  	 }
 639  
 640  	 /* Use associative array to get fields array */
 641  	function Fields($colname)
 642  	 {
 643  	 	 if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
 644  	 	 if (!$this->bind) {
 645  	 	 	 $this->bind = array();
 646  	 	 	 for ($i=0; $i < $this->_numOfFields; $i++) {
 647  	 	 	 	 $o = $this->FetchField($i);
 648  	 	 	 	 $this->bind[strtoupper($o->name)] = $i;
 649  	 	 	 }
 650  	 	 }
 651  
 652  	 	  return $this->fields[$this->bind[strtoupper($colname)]];
 653  	 }
 654  
 655  
 656  	function _initrs()
 657  	 {
 658  	 global $ADODB_COUNTRECS;
 659  	 	 $this->_numOfRows = ($ADODB_COUNTRECS) ? @odbc_num_rows($this->_queryID) : -1;
 660  	 	 $this->_numOfFields = @odbc_num_fields($this->_queryID);
 661  	 	 // some silly drivers such as db2 as/400 and intersystems cache return _numOfRows = 0
 662  	 	 if ($this->_numOfRows == 0) $this->_numOfRows = -1;
 663  	 	 //$this->useFetchArray = $this->connection->useFetchArray;
 664  	 	 $this->_has_stupid_odbc_fetch_api_change = ADODB_PHPVER >= 0x4200;
 665  	 }
 666  
 667  	function _seek($row)
 668  	 {
 669  	 	 return false;
 670  	 }
 671  
 672  	 // speed up SelectLimit() by switching to ADODB_FETCH_NUM as ADODB_FETCH_ASSOC is emulated
 673  	function GetArrayLimit($nrows,$offset=-1)
 674  	 {
 675  	 	 if ($offset <= 0) {
 676  	 	 	 $rs = $this->GetArray($nrows);
 677  	 	 	 return $rs;
 678  	 	 }
 679  	 	 $savem = $this->fetchMode;
 680  	 	 $this->fetchMode = ADODB_FETCH_NUM;
 681  	 	 $this->Move($offset);
 682  	 	 $this->fetchMode = $savem;
 683  
 684  	 	 if ($this->fetchMode & ADODB_FETCH_ASSOC) {
 685  	 	 	 $this->fields = $this->GetRowAssoc();
 686  	 	 }
 687  
 688  	 	 $results = array();
 689  	 	 $cnt = 0;
 690  	 	 while (!$this->EOF && $nrows != $cnt) {
 691  	 	 	 $results[$cnt++] = $this->fields;
 692  	 	 	 $this->MoveNext();
 693  	 	 }
 694  
 695  	 	 return $results;
 696  	 }
 697  
 698  
 699  	function MoveNext()
 700  	 {
 701  	 	 if ($this->_numOfRows != 0 && !$this->EOF) {
 702  	 	 	 $this->_currentRow++;
 703  	 	 	 if( $this->_fetch() ) {
 704  	 	 	 	 return true;
 705  	 	 	 }
 706  	 	 }
 707  	 	 $this->fields = false;
 708  	 	 $this->EOF = true;
 709  	 	 return false;
 710  	 }
 711  
 712  	function _fetch()
 713  	 {
 714  	 	 $this->fields = false;
 715  	 	 if ($this->_has_stupid_odbc_fetch_api_change)
 716  	 	 	 $rez = @odbc_fetch_into($this->_queryID,$this->fields);
 717  	 	 else {
 718  	 	 	 $row = 0;
 719  	 	 	 $rez = @odbc_fetch_into($this->_queryID,$row,$this->fields);
 720  	 	 }
 721  	 	 if ($rez) {
 722  	 	 	 if ($this->fetchMode & ADODB_FETCH_ASSOC) {
 723  	 	 	 	 $this->fields = $this->GetRowAssoc();
 724  	 	 	 }
 725  	 	 	 return true;
 726  	 	 }
 727  	 	 return false;
 728  	 }
 729  
 730  	function _close()
 731  	 {
 732  	 	 return @odbc_free_result($this->_queryID);
 733  	 }
 734  
 735  }